Reputation: 55
I'm desperately trying to get specific information from the selected rows using datatable jquery. I want to get the data-ID attribute of the second column, but I only have access to the content of the cell.
var rows = table.rows({selected: true} ).data().toArray();
for (let i in rows) {
console.log(JSON.stringify( rows[i][1] )); //shows Jason
}
<tr>
<th>Customer name</th>
<th>City</th>
<th>Amount</th>
</tr>
<tr id="1234">
<td data-id="553">Jason</td>
<td>Paris</td>
<td>$140</td>
</tr>
A solution?
Upvotes: 0
Views: 3030
Reputation: 21909
You can access the <tr>
node of each row using row().node()
.
Once you have the <tr>
node, you can access its ID:
table.rows( {selected: true} ).every( function () {
rowNode = this.node();
console.log( rowNode.id );
} );
There are different ways to iterate over your data - and I'm not sure how you are handling your "selected" rows in your specific case. But row().node()
gives you what you need.
As an alternative, for multiple rows at once, there is rows().nodes()
.
You can use something like this jQuery approach to build an array of IDs, also.
But all these variations use the DataTables row-related node()
or nodes()
API call. This means you can process the entire table - including rows which are not displayed (e.g. due to pagination).
Upvotes: 1