TIP: adding negation to regexp filters
TIP: adding negation to regexp filters
Some regexp systems allow you to use the ! as a negation operator, i.e. "!closed" to exclude matches of "closed". This is a nice tool to have for filtering columns that represent various statuses and something one of my customers wanted. The problem is that Javascript's RE does not support this operator (at least on the browsers I tested).
There is a regexp that does work in Javascript (aka "negative lookahead" http://stackoverflow.com/a/1240365/773522): ^(?!.*closed) ... but that's too much to expect most people to be able to remember.
My approach was to parse the user input and convert the simplified "!closed" into "^(?!.*closed)" for the customer.
-------------
So for reference, you can test this on http://datatables.net/release-datatables/examples/api/regex.html
Note that you can enable regexp filtering for column 2 and filter on "camino", but "!camino" does not work, though "^(?!.*camino)" does.
-------------
Here's a quick bit of code that parses the pattern and, if it begins with !, converts it to the more complicated form. I use this in a keyup handler for column filters placed in the thead similar to the code here: http://datatables.net/release-datatables/examples/api/multi_filter.html
[code]
$(".data-table thead input").keyup( function () {
/* Filter on the column (the index) of this element */
// special case, we are adding our own faux-regexp to get an inverse match if the pattern starts with !
pattern = this.value.trim();
if (pattern.substring(0,1) == "!") pattern = "^(?!.*" + pattern.substring(1) + ")";
oTable.fnFilter( pattern, $(".data-table thead input").index(this), true, false);
} );[/code]
Of course you can use this approach to make other regexp/pattern replacements to simplify the user experience in your datatables. Got any other ideas?
There is a regexp that does work in Javascript (aka "negative lookahead" http://stackoverflow.com/a/1240365/773522): ^(?!.*closed) ... but that's too much to expect most people to be able to remember.
My approach was to parse the user input and convert the simplified "!closed" into "^(?!.*closed)" for the customer.
-------------
So for reference, you can test this on http://datatables.net/release-datatables/examples/api/regex.html
Note that you can enable regexp filtering for column 2 and filter on "camino", but "!camino" does not work, though "^(?!.*camino)" does.
-------------
Here's a quick bit of code that parses the pattern and, if it begins with !, converts it to the more complicated form. I use this in a keyup handler for column filters placed in the thead similar to the code here: http://datatables.net/release-datatables/examples/api/multi_filter.html
[code]
$(".data-table thead input").keyup( function () {
/* Filter on the column (the index) of this element */
// special case, we are adding our own faux-regexp to get an inverse match if the pattern starts with !
pattern = this.value.trim();
if (pattern.substring(0,1) == "!") pattern = "^(?!.*" + pattern.substring(1) + ")";
oTable.fnFilter( pattern, $(".data-table thead input").index(this), true, false);
} );[/code]
Of course you can use this approach to make other regexp/pattern replacements to simplify the user experience in your datatables. Got any other ideas?
This discussion has been closed.