Get last row

Get last row

noreynorey Posts: 2Questions: 1Answers: 0

How can I get the last row ? I am on v 1.10.11

I thought the rows().indexes() might return an integer array and I can just get the last item, but that wasn't the case. So any hints?

Answers

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    Use the selector-modifier options to tell DataTables what order to give you the data in - for example table.rows( { order: 'applied' } ).indexes() and you can take the last row from that array.

    If you just want to get the last row immediately use the :last selector for the row-selector - i.e.: table.row( ':last', { order: 'applied' } ).

    Example: http://live.datatables.net/zurisoxe/1/edit

    Regards,
    Allan

  • noreynorey Posts: 2Questions: 1Answers: 0
    edited April 2016

    Thanks.

    What I'm trying to do is get the last row of a table, edit each item to replace a name/id with the same value and an incremented counter.

    $('.add-row').click(function(){
                last_row = dt.row(':last').data();
                console.log(last_row[0]);
                // edit last_row[0]
                dt.row.add([last_row[0],last_row[1],last_row[2],last_row[3]]).draw();
            });
    

    My question, is how can I edit each last_row[i] ? how can I use selectors on e.g. last_row[0] and fetch/edit some attributes? is there something in datatables that can make it faster?

    Thanks

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    I'm afraid I'm not quite understanding. In the above code you are using row.add() to add a new row. If you want to update the data in an existing row use row().data() (which can be used as both a getter and setter).

    Allan

This discussion has been closed.