Search code examples
javascriptjqueryfiltertablesorter

How to filter jQuery Tablesorter by multiple columns using OR logic


I am looking for a means of doing an OR filter against multiple columns in jQuery TableSorter much like DataTable Multiple column OR filter. I upgraded to 2.21.5.

I have a fiddle example. I have tried to do filter_functions:

    filter_functions: {
        '.filter-OR': function (e, n, f, i, $r, c) {
           /*
            manually check the row for all columns
            with a class of filter-OR
           */
        }
    }

but applying a function to any classes overrides the filter-availOnly option.

Not sure how to move forward with this.


Solution

  • If I am understanding the request, maybe try something like this (demo):

    $(function () {
        var $table = $('table'),
            // get 'filter-OR' column indexes
            orColumns = $('.filter-OR').map(function (i) {
                return this.cellIndex;
            }).get(),
            filterfxn = function (e, n, f, i, $r, c) {
                var col, v,
                    showRow = false,
                    content = [];
                $r.children().filter(function (indx) {
                    if (orColumns.indexOf(indx) > -1) {
                        v = (c.$filters.eq(indx).find('.tablesorter-filter').val() || '').toLowerCase();
                        showRow = showRow || $(this).text().toLowerCase() === v;
                    }
                });
                return showRow;
            };
    
        $table.tablesorter({
            theme: 'green',
            widgets: ['uitheme', 'zebra', 'columns', 'filter'],
            sortReset: true,
            sortRestart: true,
            widthFixed: true,
            widgetOptions: {
                filter_functions: {
                    '.filter-OR': {
                        'Yes': filterfxn,
                        'No' : filterfxn
                    }
                }
            }
        });
    });