Jquery-datatables multi-column sort direction

Using jquery-datatables.

Example: http://jsfiddle.net/b2fLye17/17/

$('#example').DataTable({ filter:false, columnDefs: [ { targets: [1],//when sorting age column orderData: [1,2] //sort by age then by salary } ] }); 

When you click the age column, the table is sorted by age, and then by salary in ascending order.

What will be my options to sort by age, and then by lowering wages?

Thanks!

-------------------------- Edit 1 ------------------- -

Explanation: When an age column is sorted in ascending order, it should be sorted by age, and then by lowering the salary. When an age column is sorted in descending order, it should be sorted by age, and then by salary in ascending order.

-------------------------- Edit 2 ------------------- -

Image of the desired result enter image description here

+6
source share
4 answers

Here he is. It is a bit hacked, but I wasted hours trying to figure out the same end goal - sorting two columns. http://jsfiddle.net/b2fLye17/23/

 <td data-age="40">$320</td> //In custom sort: var value = parseInt($(td).attr('data-age') + pad(td.innerHTML.substring(1), 10, '0')); 

Concept: I did not understand the way to access other cells outside the column in the foreach loop, so I added the "data-" attribute to the cell that we want to sort. This data attribute has the same meaning as the other sort column we care about ... so there is some duplicate data until we figure out how to access other β€œadjacent” cells in the loop.

I combined the two values ​​(hidden attribute and visible value) and then converted back to an int index. Since the values ​​have different lengths, I filled the second column with zeros (4086 and 40086).

+1
source

Use

 $(document).ready(function() { $('#example').DataTable({ filter:false, columnDefs: [ { orderData: [[1, 'asc'], [2, 'desc']]//sort by age then by salary } ] }); }); 

JS Fiddle http://jsfiddle.net/b2fLye17/13/

+3
source

You can do var row = settings.aoData._aData[i]; to get all the data from the row and combine it with the j0xue solution so that you can sort by another column without adding a property to html.

+1
source

Thank you for asking this question, I also encountered the same problem, after which I decided as below

 var oTable=$('#example').dataTable({ filter:false }); oTable.fnSort( [[1,"asc"], [2,"desc"]]); 

hope this is helpful

+1
source

Source: https://habr.com/ru/post/979786/


All Articles