简体   繁体   中英

jQuery DataTables Ordering By 2 columns

I am using jQuery DataTables to style and give functionality to one of my tables:

在此处输入图像描述

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

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
  • first column is secondly sorted alpha, with respect of second column order

Here is a demo -> http://jsfiddle.net/0f9Ljfjr/949/ position is sorted first, then name is sorted within each position "type".

Try this on for size

"columns": [
    { "orderData": [1,0] },

https://datatables.net/examples/basic_init/multi_col_sort.html

In my case, what helped me is this. I used data-order attribute to sort my tables.

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

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