Third Click - No Sort
Third Click - No Sort
pybcg
Posts: 41Questions: 10Answers: 0
in DataTables
https://datatables.net/forums/discussion/35977/third-click-on-sort-for-no-sort
Just wanted to follow up here - was this added ever? Or is this functionality that still does not exist?
This discussion has been closed.
Answers
No, it wasn't added, it's still in the same state as when the OP was asked.
Colin
How would you say is a good way to implement this if needed? I think the only step I'm not sure of is how to track if we clicked a header 3 times. After that, I assume if it's the third, you'd run the api.fnSortNeutral()?
Using api.fnSortNeutral() is an interesting idea.
I'm not sure if this is a good way but this is how I would approach it. First I would unbind the Datatables sorting click event to manage the sorting in my own click event handler. The function should handle the case of using shift-click to sort multiple columns. Here is my example:
http://live.datatables.net/makitoli/1/edit
Basically it gets the current sort order using
order()
. It loops through this array to populate a new array. It looks for a matching column index to the one clicked. If found it will changeasc
todesc
and append to the new array. If the column is currentlydesc
then it is callapi.fnSortNeutral()
. If its not found it will append to the array withasc
. All other columns found in theorder()
array will be appended to the new array if the shift key is held. After the loop it will isorder()
with the new array to reorder the table.This all seems to work. Consideration for column visibility and column reorder will need to be tested and changes may need to be made to the column indexes.
Note that the behavior of using shift-click with a default Datatable is a bit different than with this code. You can see this with this basic example. Sort by the Office column once so you see all of the Edinburgh rows. Hold shift and click the Position column twice so its sorting DESC. Click it again while holding shift and now that column is neither ASC or DESC but the order of the table remains in DESC order for that column. With my example the table will be reset to loaded order then reorder by the remaining sorted columns, so the Position column would be reset.
Let us know if you find any issue with your testing of this code.
Kevin
Thanks, Kevin! I will give it a shot.