简体   繁体   中英

jquery-datatables multi-column sort direction

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 result 在此输入图像描述

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/

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

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM