Search code examples
javascriptjquerytablesorter

Exlude part of string from filtering in tablesorter


I'm using Mottie's Tablesorter plug-in for jQuery and I have a question:

is there any simple way to exclude part of the cell from filtering?

I have something like this:

╔═══════╦═════════════╗
║ Row # ║    Value    ║
╠═══════╬═════════════╣
║ row_1 ║ 31 (22 + 9) ║
║ row_2 ║ 22 (18 + 4) ║
╚═══════╩═════════════╝

I filter value column, but I have to filter only that first value (i.e. 31, 22). When I filter this example for "22", the first row stays too, because it contains 22 in brackets. I don't want it.

Also, I can't split it into two cells. Something like <span data-filter="disable"> would be so handy here, but I haven't found anything about this.


Solution

  • You can use the textExtraction function to only grab the value from outside the parentheses (demo):

    $(function () {
        $('table').tablesorter({
            theme: 'blue',
            textExtraction: {
                1: function (node) {
                    return $(node).text().split(' (')[0];
                }
            },
            widgets: ['filter', 'zebra']
        });
    });
    

    Then to only filter parsed data, add the "filter-parsed" class name to the header:

    <table class="tablesorter">
        <thead>
            <tr>
                <th>Row #</th>
                <th class="filter-parsed">Value</th>
            </tr>
        </thead>
        <tbody>
        ...
        </tbody>
    </table>