DataTables - display array elements in 'reveal details' of table
DataTables - display array elements in 'reveal details' of table
I am working with the 'reveal details' functionality that is outlined at https://datatables.net/examples/server_side/row_details.html. In the columns definition, elements of an array are separated by commas by using code like:
Javascript:
{ data: 'features_table', render: "[, ].feature_text"},
which shows in the column each 'feature' selected for a record in a many-to-many relationship. This is handy and works very well. How can I display the same comma-separated list of values in the 'reveal details' table methodology using:
function format ( d ) {
return '<table>' +
'<tr class="table-info">' +
'<td>' + d.<data> .....
}
I think I have to establish a variable and access it from from the format function?
This question has an accepted answers - jump to answer
Answers
d
is the data in the row. Without seeing your data structure its hard to say exactly but you can access the data and use maybe use toString() or join() to create a comma separated string from an array. Before the return you can useconsole.log(d);
to see the data structure passed to the format function.Kevin
Thanks for your response. The data's format as read from the console (it shows up when I click to reveal the details) looks like:
Array(4)
0: {id: "8", feature_text: "apples"}
1: {id: "7", feature_text: "bananas"}
2: {id: "6", feature_text: "peaches"}
3: {id: "1", feature_text: "grapes"}
length:4
The data is accurate.
returns:
[object Object], [object Object], [object Object], [object Object]
You have an array of objects. You will need to iterate the array and process the objects. Maybe this code will get you started:
Kevin
Ah! Many thanks, Kevin, you helped me immensely. I did have to insert 'features_table' between the d and the [], like this:
But this is perfect and my understanding is much better. Thanks again.