Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  45] [ 3]  / answers: 1 / hits: 17458  / 10 Years ago, wed, december 17, 2014, 12:00:00

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 sort by age ascending then by salary ascending.



What would be my options to make it sort by age ascending then by salary descending ?



Thanks !



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



Clarification : When the age column is sorted ascending it should sort by age ascending then by salary descending. When the age column is sorted descending it should sort by age descending then by salary ascending



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



A picture of the desired resultenter


More From » sorting

 Answers
2

Here it is. It's slightly hacked, but I've been spending HOURS trying to figure out the same end goal - sorting off of 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 haven't figured out a way to access other cells outside of the column in the foreach loop, so I added a data- attribute to the cell that we want to sort off of. This data- attribute has the same value as the other sort column that 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) then converted back to an int to be indexed. Since the values are different lengths, I padded the second column with zeros (4086 vs 40086).


[#68460] Sunday, December 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
valentinam

Total Points: 166
Total Questions: 117
Total Answers: 81

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;