using ids() after adding new rows

using ids() after adding new rows

mason123mason123 Posts: 2Questions: 1Answers: 0

Hi
Hope someone could help. I need to be able to get a list of id's inside the table. For example if I have 10 rows I'd have a list of 10 ids which I'm using table.rows().ids().toArray(); that works but later if I do table.row.add() and then rerun the ids() it does not see the newly added row's id. How can I overcome this?

Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922
    Answer ✓

    What you described works in this example:
    http://live.datatables.net/venewuxe/1/edit

    Please post a link to your page or a test case showing the issue so we can help debug. Feel free to update my example.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mason123mason123 Posts: 2Questions: 1Answers: 0

    Thanks! That worked with your example!

    This is an example of how I did it and every time the new rows would return undefined for some reason, please see below

    (function someFunction() {
    console.log(table.rows().ids());
    var RandID = Math.floor(Math.random() * 6) + 1;
    var addedRowNode = table.row.add(
          {
           "first_name":"Bradley",
           "last_name":"Greer",
           "position":"Software Engineer",
           "office":"London",
           "start_date":"13th Oct 12",
           "salary":"$132,000"}
        ).draw();
    
        var rowNode = addedRowNode.node();
    
        rowNode.id = RandID;
    
    setTimeout(someFunction, 15000);
    })();
    

    So the fix was using

    var addedRowNode = table.row.add(
          {"DT_RowId":RandID,
           "first_name":"Bradley",
           "last_name":"Greer",
           "position":"Software Engineer",
           "office":"London",
           "start_date":"13th Oct 12",
           "salary":"$132,000"}
        ).draw();
    

    and removed

    rowNode.id = RandID;

    Hope this helps others if they come across the same thing. And thanks again for your time.

This discussion has been closed.