Search code examples
jquerytablesorter

jquery tablesorter2: How to sort the content of the generated Dropdown


I use the jquery tablesorter2 (https://mottie.github.io/tablesorter/docs/index.html) with the filter widget.

Now I want to change the sort order of the values in the generated dropdowns (NOT the table-values) form asc to desc. I have made a jsFiddle for this:

http://jsfiddle.net/ru5zLjk8/

Here you can find a discount column. I would like to have its values sorted desc, so that the biggest discount is first in the dropdown.

There are some options, but nothing seems to fit:

  // each option has an associated function that returns a boolean
  // function variables:
  // e = exact text from cell
  // n = normalized value returned by the column parser
  // f = search filter input value
  // i = column index

Maybe this is completely the wrong place to do it?

How to achive this?


Solution

  • You can use the filter_selectSource option to target that column (demo)

    filter_selectSource: {
      5: function(table, column, onlyAvail) {
        // get an array of all table cell contents for a table column
        var array = $.tablesorter.filter.getOptions(table, column, onlyAvail);
        return array.sort(function(a, b) {
          return a === b ? 0 : parseFloat(b) > parseFloat(a) ? 1 : -1
        });
      }
    }
    

    You will also need to add a "filter-select-nosort" class to the header to prevent internal sorting of the data.

    I think I'll make this easier by adding a "filter-select-desc-sort" class to change the sort.