filtertablesorterfilterfunction

Custom select filter function based on a selector instead of column number?


From the docs I read that the filter_functions could be used as follows:

filter_functions: {
        // Add these options to the select dropdown (regex example)
        2 : {
          "A - D" : function(e, n, f, i) { return /^[A-D]/.test(e); },
          "E - H" : function(e, n, f, i) { return /^[E-H]/.test(e); },
          "I - L" : function(e, n, f, i) { return /^[I-L]/.test(e); },
          "M - P" : function(e, n, f, i) { return /^[M-P]/.test(e); },
          "Q - T" : function(e, n, f, i) { return /^[Q-T]/.test(e); },
          "U - X" : function(e, n, f, i) { return /^[U-X]/.test(e); },
          "Y - Z" : function(e, n, f, i) { return /^[Y-Z]/.test(e); }
        }
      }

This makes a custom filter function for the 3rd column. I wonder if it's possible to specify a selector, e.g. .columnWithCoolFilteFunction instead of the column number?

The reason is that I have multiple tables where some of the columns contain similar data - and thus should be filtered in the same way - but the column number might be 1 in one table and 5 in another.

I understand that one solution would be to initiate every table with its own options, but I really like to initiate every table the same way (less code, yay!).

Possible?


Solution

  • Right now that isn't possible, but I did have the same idea! You can track this issue for when this ever gets implemented.

    But in the mean time, what you can do is set up the filter object before initializing the table:

    var alphabetical = {
        "A - D" : function(e, n, f, i) { return /^[A-D]/.test(e); },
        "E - H" : function(e, n, f, i) { return /^[E-H]/.test(e); },
        "I - L" : function(e, n, f, i) { return /^[I-L]/.test(e); },
        "M - P" : function(e, n, f, i) { return /^[M-P]/.test(e); },
        "Q - T" : function(e, n, f, i) { return /^[Q-T]/.test(e); },
        "U - X" : function(e, n, f, i) { return /^[U-X]/.test(e); },
        "Y - Z" : function(e, n, f, i) { return /^[Y-Z]/.test(e); }
    };
    
    $("table").tablesorter({
        widgetOptions : {
            filter_functions: {
                0 : alphabetical
                2 : alphabetical
            }
        },
        // other options
    });
    

    And yes, you will have to initialize each table separately until then =(