lindon
lindon

Reputation: 654

Is there a way in tablesorter to filter to select only rows where the field is empty?

When I use '|Empty' in filter_functions no filtering is applied. I'd like to be able filter so that rows where the field is empty are shown.

Upvotes: 2

Views: 693

Answers (2)

Victor Chen
Victor Chen

Reputation: 1

Thanks, Mottie. I've tried your approach above. It works fine. However, this works only when the select source function is static. The select source function was not get invoked as expected when table.trigger('update').

select source function can be dynamically invoked by table.trigger('update') if I do below:

filter_functions: {
  0: true
}

Upvotes: 0

Mottie
Mottie

Reputation: 86413

I just updated the tablesorter fork repository, so with v2.21.5+, you can now add a filter_function to target empty cells, and use the filter_selectSource to populate the select with custom and/or all column cell text as options (demo):

$(function(){
    $('table').tablesorter({
        theme: 'blue',
        widgets: ['zebra', 'filter'],
        widgetOptions: {
            filter_functions: {
                0: {
                    '{empty}' : function (e, n, f, i, $r, c) {
                        return $.trim(e) === '';
                    }
                }
            },
            filter_selectSource: {
                0: function (table, column, onlyAvail) {
                    // get an array of all table cell contents for a table column
                    var array = $.tablesorter.filter.getOptions(table, column, onlyAvail);
                    // include the filter_function option
                    array.push('{empty}');
                    return array;
                }
            }
        }
    });

});

Upvotes: 2

Related Questions