String CustomFilter
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
Hi @clicitra, sorry for taking so long to answer. I wanted to prepare a demo but didn't have time.
- first thing first, consider that client side filtering is used only for fixed tables that do not require server side pagination. Because if the table is so big that requires server side pagination, it wouldn't make sense to filter contents on the client side, since filtering impacts on pagination (how many results matches the filters?). So, the first question is: do you require server side filtering? Link to wiki page. If the table requires server side pagination, it will send the filters automatically to the server side, including them in the POST request to fetch results: you have to implement the filtering logic on the server. If instead, the table can be filtered in JavaScript, you need to define filtering function in JavaScript.
If your table is fixed, then it should work this way:
- defining the custom filters HTML, assigning a name to the custom search input field (e.g. name="mysearch")
- in the options of the KingTable object, specify both the filtersView option like described here and the filtering function, with a name that matches the input name:
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.