简体   繁体   English

jQuery DataTables 按 2 列排序

[英]jQuery DataTables Ordering By 2 columns

I am using jQuery DataTables to style and give functionality to one of my tables:我正在使用jQuery DataTables为我的一张表设置样式并为其提供功能:

在此处输入图像描述

My Goal我的目标

Order by whether or not the type of funding is active or not.. which is what it is doing currently as you can see.按资金类型是否活跃进行排序。正如您所看到的,这就是它目前正在做的事情。 Now, I would like to order the Funding column alphabetically.. so my wanted outcome should be:现在,我想按字母顺序排列Funding列。所以我想要的结果应该是:

Funding One
Funding Two
Funding Three
Funding Four
Alpha
Beta
Charlie
Test
test2

Here is what I have so far my datatables script:这是我到目前为止的数据表脚本:

var codeFundingTable = $("#Code-Funding-Table").DataTable({
    "bPaginate": false,
    "bFilter": false,
    "aoColumnDefs": [
        { "bSortable": false, "aTargets": [2] },
        { "bSearchable": false, "aTargets": [2] }
    ],
    "columns": [
        { "orderData": [1] },
        { "orderData": [0] }
    ]
});

So I am first ordering by column 1 ( Active , 0-based) then by column 0 ( Funding ) but it is not doing it alphabetically.所以我首先按第 1 列( Active ,基于 0 )排序,然后按第 0 列( Funding )排序,但它没有按字母顺序排序。

How can I make this happen?我怎样才能做到这一点?

It is a guess since we have no sample data.这是一个猜测,因为我们没有样本数据。 For example, what is the value of "active" (besides a checkbox is rendered)?例如,“活动”的值是多少(除了呈现复选框)? But I believe you can just do但我相信你可以做到

var table = $('#example').DataTable({
  order: [[1, 'asc'], [0, 'asc']]
})  
  • active column is sorted first, if values is 0 and 1 asc should be used活动列首先排序,如果值为 0 和 1 则应使用 asc
  • first column is secondly sorted alpha, with respect of second column order就第二列顺序而言,第一列是第二次排序的 alpha

Here is a demo -> http://jsfiddle.net/0f9Ljfjr/949/ position is sorted first, then name is sorted within each position "type".这是一个演示-> http://jsfiddle.net/0f9Ljfjr/949/首先对position进行排序,然后在每个position “类型”中对name进行排序。

In my case, what helped me is this.就我而言,对我有帮助的是这个。 I used data-order attribute to sort my tables.我使用 data-order 属性对表格进行排序。

data-order="[[ 0, "asc" ], [ 1, "asc" ]]

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

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