Grizzly
Grizzly

Reputation: 5943

jQuery DataTables Ordering By 2 columns

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

enter image description here

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?

Upvotes: 9

Views: 16397

Answers (3)

Omair Munir
Omair Munir

Reputation: 143

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

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

Upvotes: 0

davidkonrad
davidkonrad

Reputation: 85518

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

Upvotes: 21

Bango
Bango

Reputation: 1001

Try this on for size

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

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

Upvotes: 0

Related Questions