fnFilter - all except

fnFilter - all except

jakobjakob Posts: 3Questions: 0Answers: 0
edited February 2010 in General
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

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Jakob,

    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
  • jakobjakob Posts: 3Questions: 0Answers: 0
    Allan, thank you for your reply. After much unsuccessful experimentation with different regex permutations along the lines of your example, advice from a regex-savvy helper finally produced a working regex as follows. Perhaps it is of some help to others (improvements always welcome):

    [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?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi jakob,

    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
  • rhartrhart Posts: 4Questions: 0Answers: 0
    what would be the code here for the checkbox ?
  • jakobjakob Posts: 3Questions: 0Answers: 0
    edited November 2010
    I did it a little differently for my particular situation. The HTML is very simple with a small php check if the cookie exists or not and output checked status:

    [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.
  • rhartrhart Posts: 4Questions: 0Answers: 0
    thanks. I've it working now. Also made a small modification to have it only working on a particular column.
This discussion has been closed.