Ordering multiple columns

Ordering multiple columns

jda42090jda42090 Posts: 2Questions: 1Answers: 0

I am attempting to have the user click on a column header and based on other factors sort specific columns. In other words, have the user click column 5 and through if statements sort column 1 or 2 then column 5.

So I'm using,
.on('order.dt', function (e){})
to capture the order event. I then go through my if statements and then use,
table.order([1,"asc"],[5,"asc"]).draw();
This works as expected, the table is sorted by column 1 then by column 5, asc. The arrow on both columns show that they're being sorted in asc order.

The issue that I am facing happens when you then click on those columns after the above.

If I click on column 1 (which is going to change it to desc order) and run
table.order();
it gives me the correct two element array with 1, "desc" as the contents.

However, if I click on column 5 instead (which should change it to desc order) and run
table.order();
it gives me the incorrect two element array with 5, "asc" as the contents.

Similarly, if I used
table.order([1,"asc"],[5,"asc"],[6,"asc"]).draw();
Column 1 would show it correctly as desc whereas column 5 and 6 would show incorrectly as asc.

Is it doing this because after the first sorted column it isn't truly sorted asc - just asc based on the first column?

Is there a way around having it always come up that way? As of right now if I used the order given,
table.order([1,"asc"],[table.order()[0][0],table.order()[0][1]]).draw();
it will always come up as asc.

Answers

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin

    So I'm using, .on('order.dt', function (e){}) to capture the order event. I then go through my if statements and then use, table.order([1,"asc"],[5,"asc"]).draw();

    You need to be really careful with this, you can end up with an infinite loop since calling order() (and then draw()) will result in order being triggered again!

    I assume you have that sorted since you haven't mentioned your browser is crashing, but worth pointing out!

    However, if I click on column 5 instead (which should change it to desc order) and run table.order(); it gives me the incorrect two element array with 5, "asc" as the contents.

    How are you configuring DataTables to do the two column sort on click - with columns.orderData? Can you give me a link to the page showing the issue please?

    Allan

  • jda42090jda42090 Posts: 2Questions: 1Answers: 0

    Ha, yes I fell into that infinite loop the first time playing with this.

    This is my first time using jsfiddle so hopefully it will work here.
    http://jsfiddle.net/ktqmfk33/2/

    Here is the short bit of code in the fiddle in case that doesn't work.

    $(document).ready(function() {
        $('#example').DataTable({
               "dom": 'R<"H"lfr>t<"F"ip>'
         });
    
        var table = $("#example").DataTable();
        var flag = 0;
        $('#example').on('order.dt', function (e)
        {
            //alert("Order " + table.order() + (flag ? " When ordered by code" : " When clicked")); 
            console.log("Order " + table.order() + (flag ? " When ordered by code" : " When clicked")); 
    
            if(flag == 0)
            {
                flag = 1;  // To disable infinite looping
                table.order([1,"asc"],[table.order()[0][0],table.order()[0][1]]).draw();
                flag = 0;
            }
        });
    });
    

    My goal would be to repeatedly click "column 5" and have it alternate between asc and desc while sorting "column 1" first with it staying in asc order.

    If you comment out the table.order().draw in the fiddle, then click "column 1" and shift click "column 5" repeatedly. That will rotate "column 5" between asc, desc and neutral while keeping the "column 1" order. I would like to do this programmatically, not require the user to hold shift on those columns.

    I had spent a little bit of time with orderData. However, I believe the problem I ran into using that was that it would switch the order of both columns instead of just the one, e.g., "column 1" asc, "column 5" asc would go to "column 1" desc, "column 5" desc.

This discussion has been closed.