6

Using jquery-datatables.

Example: http://jsfiddle.net/b2fLye17/17/

$('#example').DataTable({

    filter:false,
    columnDefs: [
                    {
                        targets: [1],//when sorting age column
                        orderData: [1,2] //sort by age then by salary
                    } 
                ]
});

When you click the age column, The table sort by age ascending then by salary ascending.

What would be my options to make it sort by age ascending then by salary descending ?

Thanks !

-------------------------- Edit 1 ---------------------

Clarification : When the age column is sorted ascending it should sort by age ascending then by salary descending. When the age column is sorted descending it should sort by age descending then by salary ascending

-------------------------- Edit 2 ---------------------

A picture of the desired resultenter image description here

4 Answers 4

4

Use

$(document).ready(function() {

    $('#example').DataTable({

        filter:false,
        columnDefs: [
                        {
                            orderData: [[1, 'asc'], [2, 'desc']]//sort by age then by salary
                        }
                    ]
    });
});

JS Fiddle http://jsfiddle.net/b2fLye17/13/

Sign up to request clarification or add additional context in comments.

5 Comments

It's not working as you can see in this example. jsfiddle.net/b2fLye17/9 Thanks for your time!
Removing targets wont sort salary. In the example, it looked like it was working because the salary of the people at age 40 was already sorted in the html. I switched 86$ and 170$ to test it. Example : jsfiddle.net/b2fLye17/14
Please check this out - jsfiddle.net/b2fLye17/18. So much options to set sorting )
aaSorting does the trick before i sort a column. But what i need to do is -> When the age column is sorted ascending (with a click on it) it should sort by age ascending then by salary descending. When the age column is sorted descending it should sort by age descending then by salary ascending. Also thanks again for your time! I'll add an example shortly
This is interesting, and i do not know how to configure this using DataTable options, but i have the idea for workaround. AFAIK table is sorted by first element content, even if it is hidden. So you can bind some event to sorting and create hidden input in your salary column. This input will be Salary * -1 when you need reverse order.
2

Thanks for asking this question I too faced the same problem then solved as below

var oTable=$('#example').dataTable({
filter:false
});

oTable.fnSort( [[1,"asc"], [2,"desc"]]);

hope this is helpful

1 Comment

where we configure multisorting setting in datatables ?
1

Here it is. It's slightly hacked, but I've been spending HOURS trying to figure out the same end goal - sorting off of two columns. http://jsfiddle.net/b2fLye17/23/

<td data-age="40">$320</td>
//In custom sort:
var value = parseInt($(td).attr('data-age') + pad(td.innerHTML.substring(1), 10, '0'));

Concept: I haven't figured out a way to access other cells outside of the column in the foreach loop, so I added a "data-" attribute to the cell that we want to sort off of. This data- attribute has the same value as the other sort column that we care about... so there is some duplicate data until we figure out how to access other 'adjacent' cells in the loop.

I combined the two values (hidden attribute and visible value) then converted back to an int to be indexed. Since the values are different lengths, I padded the second column with zeros (4086 vs 40086).

1 Comment

With your example, I've been able to solve my problem with some adjustements. Thank you very much² !
1

You can do var row = settings.aoData._aData[i]; to get all the data from the row and combining that with j0xue solution so you can sort by another column without adding a property in the html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.