Questions regarding sorting - sort order / initial sort column / changing data-sort value
Questions regarding sorting - sort order / initial sort column / changing data-sort value
I have a table which will have a varying number of columns, but one column will always be present albeit in a different position every time depending on the data to be displayed (eg it could be column 2, column 3, column 4 etc). This column I want to be set as the initial sorted column and sorted DESC. The data is populated with checkboxes and as a result the data-sort attribute is being used to sort the data - with 1 for checked, 0 for unchecked.
What I need to be able to do is the following and I am not sure how to achieve it on a column that could be in any position (ie have a varying index). If there was a static number of columns then this would be fine as I would be able to set the options accordingly knowing the column index, but as the column would be a different number depending on the table content then I need to reference the column accordingly. I need to:
- Set initial sorting column as this column;
Set the sort order as DESC.
UNTESTED * - I also need to change the data-sort accordingly when the checkbox is checked / unchecked. I haven't tested as yet but if an answer is available I would appreciate the feedback - does datatables pick up changes to the data-sort attribute and adjust the sorting accordingly?
TIA
This question has an accepted answers - jump to answer
Answers
Does
order()
do what you want?Kevin
@kthorngren unfortunately not as I still need to use the index of the column, which varies every time the table is created.
I not familiar with another way to set the initial column to sort. How are you applying the data-sort attribute? Maybe from that code you can get the column index.
You need to use
row().invalidate()
orcell().invalidate()
after updating to indicate the change. Then usingdraw()
Datatables will read the DOM due to the invalidated data.Kevin
@kthorngren thanks again for your response. The data-sort is a HTML5 data attribute. It allows you to set a sort value which can differ from the content of the cell (such as in my case where the cell contains a checkbox which can't be sorted, so I use data-sort set to 1 (checked) or 0 (unchecked). HTML5 data-* attributes - cell data
It appears as though I may have to look for another way (custom HTML5 data attributes maybe?) and iterate each column detecting the attributes and then using logic to apply options to the datatable such as sort column and sort order.
Thank you for the answer regarding row().invalidate() / draw(). This will work perfectly for my needs.
As I could not find any way to achieve this using datatables I have done the following:
The HTML
For columns that need to be sorted add a
data-order
attribute with 'asc' or 'desc'.The Scripting
http://live.datatables.net/ninacake/1/edit sets the sort order before the table DataTable is created. Is this what you are trying to accomplish?
@bindrid Thank you!!! This is exactly what I was looking for.