fnFilter - all except
fnFilter - all except
Hi, based on your examples, I've managed to successfully implement an fnFilter triggered and reset using a checkbox to show/hide certain rows based on a text string.
[code] /* show/hide filter */
$("#showhide").click(function() {
if ($("#showhide").is(":checked")) {
oTable.fnFilter('mytext');
} else {
oTable.fnFilter('');
}
});
[/code]
What I'd really like to do is to filter OUT these results, e.g. to filter all rows *except* the ones with the text string match. How would I best do this (ideally with code sample)?
I can achieve this by changing my table markup so that all other entries have a text string I can match against but I can't help thinking there's a more elegant way... would jquery's .not/:not help here? or perhaps a negative match regex string? Again, an example code would be most welcome.
Many thanks,
Jakob
[code] /* show/hide filter */
$("#showhide").click(function() {
if ($("#showhide").is(":checked")) {
oTable.fnFilter('mytext');
} else {
oTable.fnFilter('');
}
});
[/code]
What I'd really like to do is to filter OUT these results, e.g. to filter all rows *except* the ones with the text string match. How would I best do this (ideally with code sample)?
I can achieve this by changing my table markup so that all other entries have a text string I can match against but I can't help thinking there's a more elegant way... would jquery's .not/:not help here? or perhaps a negative match regex string? Again, an example code would be most welcome.
Many thanks,
Jakob
This discussion has been closed.
Replies
DataTables allows for regular expression filtering: http://datatables.net/examples/api/regex.html and http://datatables.net/api#fnFilter . For example enter "(M[^o])" into the regex input box on the demo page. This way you can do boolean logic.
Allan
[code] /* show/hide filter */
$("#showhide").click(function(){
if ($("#showhide").is(":checked")) {
oTable.fnFilter('',null);
} else {
oTable.fnFilter('^((?!mytext).)*$',null,false);
}
});
[/code]
One last question: I'm using "bStateSave": true, and the cookie saves the information correctly, which is great. The only problem is that my checkbox is rendered with checkbox="checked" regardless of the saved state. Is there a way in which I can detect whether a (this) filter is currently active and set the checked-status accordingly? Again, code examples welcome.
Also, is there a straightforward (datatable) way to totally reset the saved state to its initial rendered form (e.g. including other column sort-states), i.e. the equivalent of effectively removing the cookie?
Nice regular expression that :-)
1. bStateSave: What you need to do is read the status from DataTables (fnSettings) after ti has initialised to see if your checkbox should be checked. fnSettings().oPreviousSearch.bEscapeRegex will hopefully do it for you.
2. There isn't a way to clear the global and column filtering all in a single call at the moment. Sounds like a very good candidate for an API function: http://datatables.net/development/api :-). Just need to clear the global filter, and then loop over the columns as well.
Regards,
Allan
[code] />
Show hidden entries[/code]
and in the head I had following script that switched the cookie status when the checkbox is clicked:
[code]
var oTable;
$(document).ready(function()
{
/* show/hide filter */
$("#showhide").click(function(){
if ($("#showhide").is(":checked")) {
oTable.fnFilter('',null);
$.cookie('showhide_hidden',null);
} else {
oTable.fnFilter('^((?!hidden).)*$',null,false);
$.cookie('showhide_hidden','hide');
}
});
});
[/code]
EDIT: Ah yes, you need jquery.cookie.js (Klaus Hartl) for this.