Get row number in display order after sort?

Get row number in display order after sort?

Karl53Karl53 Posts: 72Questions: 29Answers: 0

DataTables 1.10.5, Editor 1.4

JavaScript array is the data source. Using inline editing.

The values change programatically in the column that the table is ordered by. The table sort works just fine.

The issue I need to resolve is how do I get the new row number after a sort by its display order? That is, I want selectedRow below to contain the row number as the user would see it. Currently, selectedRow is set to the original display row location. (Also note, the JavaScript data is maintained in the same display order and rows().invalidate() has been called, though I assume that is not necessary.) Also, using the modifier parameter did not change the result.

        // toggle the row selection - note if not using Bootstrap, then class has to be '.selected'
        $('#TVM tbody').on('click', 'tr', function () {
...
            selectedRow = table.row(this).index();
//          selectedRow = table.row(this, {order: 'index'}).index();

Thank you.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    I want selectedRow below to contain the row number as the user would see it

    Use cells().render() to have DataTables return the rendered data.

    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0

    Hi Allan,

    Thanks for the prompt reply. Unfortunately, cells().render() does not allow me to get what I need, unless I've coded it wrong.

    I want the row number after a sort, that is, the row number in display order.

    Here is how I coded it.

    $('#TVM tbody').on('click', 'tr', function () {
        if ($(this).hasClass('active')) {
    
            var selectedRow = table.row(this).index();
    
            var data = table.cells( selectedRow, '' ).render( 'display' );
    
            // selectRow to contain the row number in display order
            selectedRow = data.row(this).index();
            ...
            
    

    selectedRow after both assignments contains the same value. The original row number before the sort.

    Did I misinterpret your meaning?

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Oh I see, I completely misunderstood and the render() method won't really help you in that case.

    Try:

    var rowNumber = table.rows( { order: 'applied' } ).nodes().indexOf( this );
    

    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0

    That's what I need. Thank you.

This discussion has been closed.