Search code examples
jquerytablesorter

Tablesorter – Filter cells with multiple values


I am trying to customize the filter function of my Tablesorter table. I have a table which may in some cases contain multiple values (often one or two numbers) separated by a comma.

A column may look like this:

40
32
31,36
33
31,42
36
42
41,42

Now if I filter this column I with for example 36 - 42 I want the column with value "31,42" to remain.

I think it can be basic, just split the value and compare the input to the two values. But I can't find a suitable function to hook into. Any ideas?

Updated

I have this parser to enable sorting of my comma separated column

        $.tablesorter.addParser(
          id: 'comma-number'
          format: (s) ->
            lNumber = parseFloat(s.replace(/,/g, '.'))
            return lNumber
          type: 'numeric'
        )

What I want to do is compare the input in a filter searchbox against two values. Is there a way to get a hold of the input value in a filter event?


Solution

  • The issue here is that when tablesorter parses numeric table cell content, it ignores commas (when the usNumberFormat option is true. So the parsed result of 31,42 is 3142.

    The solution would be to set the parser on that column to text, and set the filter to "match"

    <th class="sorter-text filter-match">Numeric</th>
    

    Then in the filter, search using the " or " operator: "31 or 42" or "31|42"

    This only needs a basic initialization code (demo):

    $('table').tablesorter({
        theme: 'blue',
        widgets: ['zebra', 'filter'],
        widgetOptions : {
            filter_reset: '.reset'
        }
    });