order() api with 2d Arrays not working

order() api with 2d Arrays not working

dylanmacdylanmac Posts: 49Questions: 7Answers: 1

Hi,

I am trying to sort on multiple columns with 2D arrays using the order() api. The initial order of the table is correct. And when I click on any column I can 'asc' sort on that column. But clicking the column again will not toggle to 'desc'.

var api = $('#table').DataTable({
    "drawCallback": function (settings) {
        applyGrouping();
    }
});
var applyGrouping = function() {
    api.order( [ [ 0,'asc' ], [ 4,'asc' ], [ 5,'desc' ] ] );
}

I am not calling draw() after the api.order() because I assume the drawCallback does that already (and, in fact, when I added draw() after the api.order() I hit an endless loop).

Moreover, even a single column sort isn't allowing 'desc' sorting here either, e.g.

api.order( [ [ 0,'asc' ] ] );

Thanks.

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Can you throw this on a live.datatables.net instance so I can replicate the issue?

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    I am not calling draw() after the api.order() because I assume the drawCallback does that already

    No - the draw callback happens because of a draw - so any updates applied inside it would not update.

    The problem with calling draw() in the draw callback is, as you say, that you hit a never-ending loop.

    Have you tried using order to apply an initial order to the table (assuming that is what you want)?

    Allan

  • dylanmacdylanmac Posts: 49Questions: 7Answers: 1

    Hi Allan,

    So the example above is a much simplified version of the actual table I'm creating. I'm actually doing a row grouping on the table (similar to your row grouping in the advanced examples). The multi-column sort is built dynamically (based on runtime variables) within the same function.

    Perhaps what I need to do is separate those two functions out and call the ordering in the initial settings. I'll try that and let you know.

    Thanks.

  • dylanmacdylanmac Posts: 49Questions: 7Answers: 1

    Well that was exactly the issue. I refactored my code to set the order options in the init rather than in the drawCallback. Thanks for explaining it to me, Allan.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Awesome - thanks for posting back with the update. Good to hear yu've got it working now.

    Allan

This discussion has been closed.