Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 841
Grizzly Avatar asked Apr 17 '26 04:04

Grizzly


1 Answers

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

like image 93
davidkonrad Avatar answered Apr 23 '26 10:04

davidkonrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!