Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables selected rows data-attribute

I've got datatables set up nicely and with the select extension - I have this code for when someone uses a "report" and it loads another page using some cell content from the table to pass to the report. That works fine, however - I need to be able to pass a data-element of each <tr> of the table instead of the row data...

This is my current code:

var table = $('#table_search_project').DataTable();

//then re-run the code to get the selected rows
var id_list = $.map(table.rows('.selected').data(), function (item) {
    return item[1]
});

So you can see, I used to use item[1] to return, but I really want to be able to get the <tr> object so I can retrieve the data-something-id property (but I don't want to show it - hence the issue)

like image 384
Chris Avatar asked Aug 08 '17 08:08

Chris


People also ask

How to get data from selected row in DataTable?

It can be useful to provide the user with the option to select rows in a DataTable. This can be done by using a click event to add / remove a class on the table rows. The rows(). data() method can then be used to get the data for the selected rows.

How to select all rows in DataTable jQuery?

The selectAll button will simply select all items in the table, based on the current item selection mode ( select. items() ) - e.g. if the item selection mode is rows , all rows in the table will be selected when this button is activated.


1 Answers

I worked it out. Too hasty to ask in SO!

I used this instead:

var id_list = $.map(table.rows('.selected').nodes(), function (item) {
    return $(item).data("entity-id");
});

The nodes() collection sends me back the <tr> elements instead of the data. Exactly what I wanted. Found the information here:

https://datatables.net/reference/api/row().node()

like image 155
Chris Avatar answered Oct 07 '22 18:10

Chris