简体   繁体   English

jquery-datatables多列排序方向

[英]jquery-datatables multi-column sort direction

Using jquery-datatables. 使用jquery-datatables。

Example: http://jsfiddle.net/b2fLye17/17/ 示例: 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 --------------------- --------------------------编辑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 --------------------- --------------------------编辑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/ JS小提琴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. 它有点被黑,但我一直在花费HOURS试图找出相同的最终目标 - 从两列中排序。 http://jsfiddle.net/b2fLye17/23/ 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. 概念:我还没有找到一种方法来访问foreach循环中列之外的其他单元格,因此我向要排序的单元格添加了“data-”属性。 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. 我将两个值(隐藏属性和可见值)组合在一起,然后转换回要编入索引的int。 Since the values are different lengths, I padded the second column with zeros (4086 vs 40086). 由于值的长度不同,我用零填充第二列(4086 vs 40086)。

You can do var row = settings.aoData._aData[i]; 你可以做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. 从行中获取所有数据并将其与j0xue解决方案相结合,以便您可以按另一列进行排序,而无需在html中添加属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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