Get rows content and the relative row IDs

Get rows content and the relative row IDs

poisonspoisons Posts: 25Questions: 3Answers: 0

With the method rows().data() I can have an object containing all the rows content.
I can also iterate it with an each loop, but how to get the row id? It seems it is not collected by the method

Replies

  • kthorngrenkthorngren Posts: 21,340Questions: 26Answers: 4,954

    If the id is part of the data then row().data() will have it along with all the other data for the row whether shown in a column or not.

    You can use row().id() to get the row id, ie, the tr id assigned in the DOM. Use the rowId option to define the row id object.

    If you still have questions or issues please post a link to your page or a test case replicating the issues so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • poisonspoisons Posts: 25Questions: 3Answers: 0

    Well, this is a general question, do you need anyway the code?
    I build the table with ajax, and I assign to each datatables row a custom Id through the DT_RowId parameter.

    Once built the datatables table, I would like to use the rows().data() method to collect all the values in the table, but need to collect also the id associated with the row.

    Is there a way to get it with one call? Because I looked in the object returned from rows().data() and I could not find it.

  • kthorngrenkthorngren Posts: 21,340Questions: 26Answers: 4,954
    edited May 2023

    DT_RowId is the default row ID Datatables looks for so you don't need to set the rowId. Use the browser's network inspector to look at the XHR response. Copy one row of data and paste into the thread.

    Here is an ajax example:
    https://live.datatables.net/dodobuge/8/edit

    Use the network inspector to see the XHR response. The response has id not DT_RowId but the same idea holds true that row().data() has access to all the row data. Click a row and you will see the id and everything else. The id is not defined as one of the columns.

    Kevin

  • poisonspoisons Posts: 25Questions: 3Answers: 0

    Yes, the id is in the TR as attribute, so I could easily cycle through all the TRs with jQuery, and get the id of the TR and also all the values of the TDs

    I just wanted to know if there's a method, like rows().data() in datatables to collect also the id attribute of the TR together with all the values of the TDs. At the moment the final html table is like this

    <tr id="10" class="odd">
      <td class="sorting_1">Toast</td>
      <td>1</td>
      <td>2</td>
    </tr>
    

    I use this code to cycle, and the value returning is an array of rows, which value is the comma separated TDs content

    createButton.click(function (e) {
        e.preventDefault();
        $.each(tableProduction.rows().data(), function (key, value) {
          console.log(key+' -> '+value);
        });
    

    If I use the rows().data() method, it returns an object/array with the values of the 3 TD, is there a way to get, together with the TDs content, also the TR attr?

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    If you are using DT_RowId to populate the id attribute of the tr, then it will be part of the object for the row:

    table.row(selector).data().DT_RowId
    

    The row().id() method will also get it.

    If you want to get the id from multiple rows, either use rows().ids() or use the pluck method - e.g.:

    table.rows(selector).data().pluck('DT_RowId')
    

    Allan

    (I think I've basically just said what Kevin has already pointed out - perhaps with the addition of the pluck() method. If I've misunderstood the problem, perhaps you can modify the example Kevin gave, or create your own, that shows the issue so we can offer some more help).

Sign In or Register to comment.