Get rows content and the relative row IDs
Get rows content and the relative row IDs
poisons
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
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, thetr
id assigned in the DOM. Use therowId
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
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.
DT_RowId
is the default row ID Datatables looks for so you don't need to set therowId
. 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
notDT_RowId
but the same idea holds true thatrow().data()
has access to all the row data. Click a row and you will see theid
and everything else. Theid
is not defined as one of the columns.Kevin
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
I use this code to cycle, and the value returning is an array of rows, which value is the comma separated TDs content
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?
If you are using
DT_RowId
to populate theid
attribute of thetr
, then it will be part of the object for the row:The
row().id()
method will also get it.If you want to get the id from multiple rows, either use
rows().ids()
or use thepluck
method - e.g.: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).