Shift-click, multi-column sorting is not working in the following simple test case

Shift-click, multi-column sorting is not working in the following simple test case

tristramtristram Posts: 8Questions: 2Answers: 0

The following is a test case very similar to the DataTables "Multi-column ordering" example, but the test case fails to allow for shift-click, multi-column sorting.

http://live.datatables.net/dogitala/2/edit

Does anyone have any idea why? And/or a work around?

FWIW, I highly doubt the issue is related to the format of the JSON data.... for obvious reasons... and because my app has a simple data structure more like the DT example mentioned above. My app does define "columns" and the data for those columns.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,762Questions: 26Answers: 5,033
    edited November 2019

    Thanks for the test case! You are using the following orderData:

              { targets: 0, orderData: [0, 1] },
              { targets: 1, orderData: [1, 0] },
              { targets: 2, orderData: [2, 0] },
              { targets: 3, orderData: [3, 0] },
              { targets: 4, orderData: [4 ,0] },
              { targets: 5, orderData: [5, 0] },
    

    Column 0 has unique data so it seems like the second shift-click sorting isn't working. I changed the name Gavin Joyce to Cedric Kelly to highlight that it is working in this updated example. First sort by the Office column, you will see something like this:

    Cedric Kelly    Developer   Edinburgh   8822    2010/12/22  $92,575
    Cedric Kelly    Senior Javascript Developer Edinburgh   6224    2012/03/29  $433,060
    Dai Rios    Personnel Lead  Edinburgh   2290    2012/09/26  $217,500
    

    Now shift-click the Position column and you will see the two Cedric Kelly rows switch positions depending on the sorting direction.

    Does this answer your question?

    Would you like a different behavior?

    Kevin

  • tristramtristram Posts: 8Questions: 2Answers: 0

    Alas, even in your test case it still doesn't work as I would expect. Click the "Office" column and then shift-click the "Name" column. The Name column does not change (but the Position column changes!). It easiest to recognize if the Name column is sorted in the ascending order. Clicking Name and then shift-clicking the Extension column also shows the sort fails. (etc)

  • tristramtristram Posts: 8Questions: 2Answers: 0

    It just dawned on me that I have been misunderstanding how the expected behavior should be.

    I am wanting to have a default behavior where, for instance as in the test case, if Office is clicked on, it is sorted by Office, then Name. If Office is clicked on, then Name is shift-clicked, it is sorted with the before mentioned default sort... until Name is shift-clicked again... in which case it is now sorted in descending by Office (ascending) and Name descending.

    Sorry for my confusion. Is there a work-around for this?

    (as an aside: perhaps the "Multi-column ordering" docs and test data could speak to this issue?)

  • tristramtristram Posts: 8Questions: 2Answers: 0
    edited November 2019

    ... and, btw, thank you for your clarification. I was thrown by the first column default sort.

    If I am not mistaken, the DT shift-click sort implementation is a cascade of the sorts, and because there are default sorts composed of multiple columns for all of the columns, a single click then shift-click on a another columns creates a cascade of 3 or more sorts. For example, clicking the Office column and then clicking the Extn column leads to a Office-Name-Extn-Name sort. I throw in the last "-Name" more as a question, than as a statement.

    Put another way, is the sort [Clicked column default columns sort results][Shift-clicked default columns sort], or is it [Clicked column sort results]][Shift-clicked columns default sort]?

    In any case, I suspect that the current DT handling of the sort in the test case above will prove challenging for the end-user. Perhaps it might be a good idea to provide an option to change the behavior and allow Shift-click multi-column sorting to abandon the multi-column default behavior, and fall back on single selected column sorts. So in the above case, when Office is clicked, and then Extn is shift-clicked, the result order would be an ascending Office sort and then a secondary ascending Extn sort (just as if there were no orderDatas defining multi-column sorts). If Extn is shift-clicked again, the sort would be ascending Office, descending Extn. (I am using Extn in this example instead of the more obvious Name, because name is already in the default sort for the Office column.... but if the user had instead shift-clicked the Name column and then shift-clicked it again, it would sort in an ascending Office, then descending Name order).

    Is there a work around to obtain the behavior I am desiring? Thanks!

  • allanallan Posts: 64,106Questions: 1Answers: 10,574 Site admin
    Answer ✓

    If I am not mistaken, the DT shift-click sort implementation is a cascade of the sorts

    Yes - it is a cumulative sort. So if we consider a sort on column index 2 it will actually be sorting on 2 then 0. If the user then adds column index 1 to the sort using shift click it would sort in column index order 2, 0, 1. As Kevin says, 0 contains unique values, so column index 1 will never be sorted.

    There is no built in way to do what you are looking for. What I think you would need to do is listen for the order event listener (and remove the orderData arrays in the init). Then check in the event handler if multi-column sorting is used or not. if not, then trigger another order action, adding your default column to the order array. If multi-column sorting is being used, then just let that run through (i.e. do nothing in the event handler).

    Allan

This discussion has been closed.