10

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?

3
  • What happens if you switch the orderData 1 and 0? Does it order by Funding only? Commented Jul 21, 2017 at 16:47
  • @Bango correct, it orders alphabetically without any regard for being active or not Commented Jul 21, 2017 at 16:53
  • can you create a fiddle with sample data..? Here is a starting point -> jsfiddle.net/0f9Ljfjr/948 Commented Jul 22, 2017 at 9:13

3 Answers 3

22

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try this on for size

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

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

2 Comments

sorry, this doesn't help. It would be the same as { "orderData": [0] }, { "orderData": [1] }
It 'would be' the same, if you did try it; or you did try it, and it is the same? Also note that your Active column contains HTML which oftentimes interferes with functionality like searching and sorting.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.