Third Click - No Sort

Third Click - No Sort

pybcgpybcg Posts: 41Questions: 10Answers: 0

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?

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    No, it wasn't added, it's still in the same state as when the OP was asked.

    Colin

  • pybcgpybcg Posts: 41Questions: 10Answers: 0

    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()?

  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,735
    edited December 2020

    Using api.fnSortNeutral() is an interesting idea.

    How would you say is a good way to implement this if needed?

    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 change asc to desc and append to the new array. If the column is currently desc then it is call api.fnSortNeutral(). If its not found it will append to the array with asc. All other columns found in the order() array will be appended to the new array if the shift key is held. After the loop it will is order() 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

  • pybcgpybcg Posts: 41Questions: 10Answers: 0

    Thanks, Kevin! I will give it a shot.

This discussion has been closed.