How do I retrieve multiple column values from selected rows to a json array?
How do I retrieve multiple column values from selected rows to a json array?
Link to test case:
I have a data table rendered from an SQL query. The data looks somewhat like
| EIN | Name | PRAN | FVU | Month | Year |
| 1234 | Jones | 09878 | samplefvu0.fvu | 4 | 2022 |
| 2314 | Sarah | 89768 | samplefvu1.fvu | 3 | 2022 |
| 6578 | Peter | 89768 | samplefvu2.fvu | 5 | 2022 |
Description of problem:
Here I want to select the values in columns EIN
, Month
and Year
from rows 1 and 3 (i.e. 1234, 4, 2022 and 6578, 5, 2022) as a json array like
{
[
[1234, 4, 2022], [6578, 5, 2022]
]
}
For that, I have used row selection and implemented something like this so far
$('#detailTable').DataTable({
destroy: true,
paging: true,
responsive: true,
select: {
style: 'multi',
},
scrollY: 400,
scrollCollapse: true,
autoWidth: false,
data: data,
dom: 'Bfrtip',
"rowId": 'ein',
"columns": [
{ title: "Ein", "data": "ein" },
{ title: "Name", "data": "emp_name" },
{ title: "PRAN", "data": "pran_no" },
{ title: "FVU", "data": "fvu_file_name" },
{ title: "Month", "data": "sal_month" },
{ title: "Year", "data": "sal_year" },
],
stateSave: true,
// rest of code
Can anyone help me towards achieving the desired result? I have tried previously using checkboxes but with no success.
Thanks for reading. Any help is much appreciated.
This question has an accepted answers - jump to answer
Answers
This example shows how to get selected rows. Use
rows().data()
to get the selected row data into an array.Kevin
@kthorngren Thank you for the suggestion. I did manage to solve it by using jquery and CSS class
.selected
with rows APII would actually suggest you do:
The difference is that
tr.selected
as a selector will only pick out selected rows on the current page.deferRender
could also cause issues if you did something like a select all and not all rows had been rendered.See the docs here for further information.
Allan