7

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)

1 Answer 1

17

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()

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, very useful. Sometimes it's pretty tricky to find exactly how to use the pretty complex data model of DT, nodse(), node(), $(this), data() etc... This was the right combination!
Tried this but finally had to change it the last line to target attr to get it to work return $(item).attr("data-id"). Still very useful
.data() has been in jQuery since 1.2.3 - what version are you running?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.