RobertoPrevato / jQuery-KingTable

A jQuery plugin for administrative tables that are able to build themselves, on the basis of the input data.
MIT License
117 stars 33 forks source link

String CustomFilter #2

Closed clicitra closed 8 years ago

clicitra commented 8 years ago

Hi,

I'm looking at your work, it's really great! I have a problem, I try to set myself a simple customFilter, which filters a string on a single column, and I can not, it does not work :-( I did not really have Js skill :(' , I am rather PHP

Can you help me?

Thank you

RobertoPrevato commented 8 years ago

Hi @clicitra, sorry for taking so long to answer. I wanted to prepare a demo but didn't have time.

If your table is fixed, then it should work this way:

    filters: {
      //NB: the keys of filters object must match the name property of input elements
      //that are defined in the filters view
      "mysearch": function (filter, obj) {
         if (!filter) return true;
         //obtain a regular expression from the string filter inserted by the user:
         var regexUtils = R("regex");
         var escapedFilter = regexUtils.escapeCharsForRegex(filter);
         var rx = regexUtils.getSearchPattern(escapedFilter);
         return !!rx.exec(obj.yourPropertyWhereToSearch);
      }

The filtering function runs synchronously in this case, so it must return true or false. What the function above is doing, it is using the functions I already wrote to escape characters for Regular expressions, then creating a regular expression (by default, case insensitive), and looking for matches inside the property called "yourPropertyWhereToSearch" of the object passed to the function - each item in the table -. Hope this helps, please let me know.