Third Click - No Sort

Third Click - No Sort

pybcgpybcg Posts: 41Questions: 10Answers: 0

Just wanted to follow up here - was this added ever? Or is this functionality that still does not exist?


  • colincolin Posts: 12,098Questions: 0Answers: 2,054

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


  • 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: 13,112Questions: 25Answers: 3,081
    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:

    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.


  • pybcgpybcg Posts: 41Questions: 10Answers: 0

    Thanks, Kevin! I will give it a shot.

Sign In or Register to comment.