Sorting rows of a nested table within a DataTables Child-row
Sorting rows of a nested table within a DataTables Child-row

I am utilizing the child-row ability (https://datatables.net/examples/api/row_details.html of DataTables and have a successful bit of JS code that uses nested arrays within a json to populate.
For the fiddle I am just using random generated numbers rather than live data.
I am brand new to Javascript and wondering if Datatables or JQuery may have a built in sorting function for what is essentially a nested table?
If not - where do I start adding the code? On Show or as part of the function? I am looking sort by the order of the random number column and they do not need to be manually sortable.
Can anyone help?
My Fiddle:
live.datatables.net/benilapu/1/edit
This question has an accepted answers - jump to answer
Answers
The easiest way is to add the child row data you want to sort into hidden columns using
columns.visible
. Then you can useorder()
to sort by the hidden column or columns.Kevin
Hi @kthorngren - thanks for your answer. Why hidden columns? As per the table example, I want to sort by numbers that are visible.
does this help?
I have been trying for hours to understand how I can do this.... if anyone can save me from throwing my laptop out of the window, I'd be eternally grateful!
Trying to sort the Child rows as per the comments on the fiddle:
live.datatables.net/sevikiki/1/edit
I think I misunderstood the question. I thought you wanted to sort the Datatable rows by the values in the child. It sounds like you want to sort the child rows themselves. Are you wanting to control the sorting by using the sorting icons in the
value
column header?Dataables doesn't do anything with the child rows except show and hide them. The layout, order, etc is up to you. How you do this depends on what your data structure is. Here is an example using an array of arrays and the Javascript sort() method with a function.
http://live.datatables.net/sevikiki/2/edit
If interested you can use
order()
to get the table ordering to determine which way to sort the child rows.Kevin
I think this is exactly what I am looking for @kthorngren ! Thank you so much!