Is it possible to automatically populate the childrows section of a datatable?
Is it possible to automatically populate the childrows section of a datatable?
I'm currently learning how to use DataTables and trying to create a table with child rows nested inside an accordion button.
Each of the rows in the datatable has to be able to display a different number of childrows when necessary - so for example, row 1 has 2 child rows, row 2 has 4 child rows, and row 3 has 1 child row.
The documentation for child rows here (https://datatables.net/examples/api/row_details.html) uses a JS return statement to create a table that calls for specific variables from the data array. Is there any way to access the returned table after it's created so that I can run an additional function on it? Otherwise, is there a way to change the childrow formatting so that I can populate it manually or automatically with a different number of lines for each main row?
Here's a full working demo JSFiddle for reference: https://jsfiddle.net/mosespak/1f1nLwtz/8/
This question has accepted answers - jump to:
Answers
The format() function is just an example. You have full control of how the data is formatted and returned. All that matters is you pass into the child() method the desired HTML formatted data.
Kevin
@kthorngren Thanks for the very prompt response! Do you have any suggestions on how to pass the desired formatted data? It just clicked that I don't need to use the format() function, but I'm unsure on how to change the JS so that I can set the number of lines for several different rows.
I'm looking here (https://datatables.net/reference/api/row().child()) and wondering if I can combine bindred's example and the multiple rows example together somehow.
Hi @mosespak ,
As @kthorngren said, the
format()
function is purely an example. In this hacked together example based on the first first example, the number of child rows increasing with every click (until 5, then it's reset to 1). You can do whatever you like in your format function - the logic depends on your code.Hope that helps,
Cheers,
Colin
Thanks @colin! That's exactly the kind of example I needed to better understand how to make changes. Do you know if there are any other hacked examples I can look through? How did you find that one too, if you don't mind me asking?
Hi @mosespak,
My example is just a hack of the example you gave, created to show you variable length children I don't know of any others specific to child rows, but you could take a ganders through the examples section, and the reference pages, normally there are examples for most things you need.
Cheers,
Colin
Sounds great. Thanks for taking the time to create an example and responding, @colin. It's super appreciated.