Sort on click
Sort on click
madvora
Posts: 11Questions: 4Answers: 0
I have a table with these fields, ID, FULL_NAME, LAST_NAME.
ID and FULL_NAME are displayed, LAST_NAME is hidden.
On initialization, I have the table default sorted by LAST_NAME (the hidden column.)
I want to sort the table on LAST_NAME when the user clicks on FULL_NAME.
I think I can grab the index of the header on the click and then manually sort by another field.
How can I do this?
$('#table1').on('click', 'thead th', function(event) {
var clickedHeader = $(this).closest('th').index();
if (clickedHeader == 1) {
//sort by header index 2 (LAST_NAME)
}
});
This discussion has been closed.
Answers
I figured I could use a variable to represent the sort order, then just pass it in with different values, once I click on the column I want changed. But, how do I get it to refresh?
defaultSort = [[ 7, "asc" ]];
oTable1 = $('#table1').dataTable( {
"order": defaultSort
});
I ended up using a method similar to this
http://jsfiddle.net/bHKNQ/1/
However, I'm just destroying and recreating the table with the default sort, if they click on that column header