MultiColumn Ordering - LImit to only 2 columns at a time

MultiColumn Ordering - LImit to only 2 columns at a time

smoldovanskiysmoldovanskiy Posts: 59Questions: 7Answers: 0

Is there a way to limit the multi column ordering to only 2 columns at a time? On the client side, I want to allow users to sort up to 2 columns at a time.

This question has accepted answers - jump to:

Answers

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

    There's no restriction - it's enable all sorting, or have none at all - so I can't think of a way to enforce that, I'm afraid,

    Colin

  • smoldovanskiysmoldovanskiy Posts: 59Questions: 7Answers: 0

    I would love this as an enhancement, a setting to specify max number of sort columns

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Answer ✓

    Hi,

    It is a somewhat cheeky way to do it, but you can use a listener on the draw event and then use the API to check the current ordering. If there are more than 2 columns, then stop it from doing that:

      table.on('draw', function () {
        let order = table.order();
        
        if (order.length > 2) {
          table.order([order[0], order[1]]).draw();
        }
      });
    

    http://live.datatables.net/buhuhari/1/edit

    It isn't quite right, since it does involve an extra draw which might cause a problem if server-side processing is being used? preDraw might be useful for that.

    Can I ask, what is the use case for this? I don't think we've been asked for this feature before.

    Regards,
    Allan

  • smoldovanskiysmoldovanskiy Posts: 59Questions: 7Answers: 0

    It is server side so with each extra layer of sorting code logic becomes more complicated.

    I want it limited to just the two columns as per user requirement, they need to sort by date and then by and index number.

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736

    If you want to limit sorting to two specific columns you can use columns.orderable.

    Kevin

  • smoldovanskiysmoldovanskiy Posts: 59Questions: 7Answers: 0

    I want them all to be orderable, but I need to limit the ordering to two columns at a time or less.

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin

    You could pre-process the order array in your server-side processing script and just remove the items in the array after index 1. The UI wouldn’t quite be right, since it would still show the sorting arrows as active on the third, fourth, nth, columns - but it wouldn’t sort.

    Unfortunately, what you are looking for is not a built in feature of DataTables.

    Allan

Sign In or Register to comment.