最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery-datatables multi-column sort direction - Stack Overflow

programmeradmin4浏览0评论

Using jquery-datatables.

Example: /

$('#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

Using jquery-datatables.

Example: http://jsfiddle/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

Share Improve this question edited Dec 17, 2014 at 19:41 sebmoi asked Dec 17, 2014 at 13:55 sebmoisebmoi 631 gold badge1 silver badge5 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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/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/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 bined 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 bining that with j0xue solution so you can sort by another column without adding a property in the html.

发布评论

评论列表(0)

  1. 暂无评论