Sort on click

Sort on click

madvoramadvora Posts: 11Questions: 4Answers: 0
edited December 2014 in Free community support

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)  
          }
}); 

Answers

  • madvoramadvora Posts: 11Questions: 4Answers: 0

    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
    });

  • madvoramadvora Posts: 11Questions: 4Answers: 0

    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

This discussion has been closed.