-->

TableSorter. Sort by Dropdown selection

2020-03-26 08:02发布

问题:

I have a simple table that contains

ID and Name (FirstName + LastName) fields. Above the table I have a dropdown list with Options ID, FirstName , LastName. Based on the selection of dropdown Table should sort. I dont know How to trigger tablesort sort function based the selection.

回答1:

Maybe this is what you wanted (demo)?

HTML

<select>
    <option value="-">Choose a column</option>
    <option value="0">column 1c</option>
    <option value="1">column 2</option>
    <option value="2">column 3</option>
    <option value="3">column 4</option>
</select>

<table class="tablesorter">
    <!-- stuff here -->
</table>

Script

$(function(){
  $('table').tablesorter();

  $('select').change(function(){
    var column = parseInt($(this).val(), 10),
      direction = 1, // 0 = descending, 1 = ascending
      sort = [[ column, direction ]];
    if (column >= 0) {
      $('table').trigger("sorton", [sort]);
    }
  });
});