fnFilter - switch to "OR" manner

fnFilter - switch to "OR" manner

filuSfiluS Posts: 4Questions: 0Answers: 0
edited September 2011 in General
I am kinda new into datatables but I decided to use them for my current project. Right now I am experiencing one problem. I want to make a filter where I can set various filters for views (date ranges, status/priority restrictions etc) and one thing I am trying (and failing) to achieve is being able to make some filters work in "OR" manner across multiple columns. To give you a better picture here is scenario:

1. user loads page and datatable loads data from server via ajax, which includes title, description, some dates...
2. user decides to filter view by settings filter inputs. In this case, there is a text input where user can put desired text he/she wants to filter out from a title + there is checkbox to include/exclude filtering inside description too (which is in another column)
3. if user ticks "Include description", another fnFilter is added to datatable, but that means rows have to pass filtering on title AND description at the same time.

Problem is, I want it to be that given string is present either in title or in description, doesnt have to be in both. I looked at fnFilter and it seems it doesnt work that way. Is it possible to achieve this kind of filtration without using server-side processing?
And also, I dont really want to go in way of adding new filtering function into $.fn.dataTableExt.afnFiltering object because afaik its general for all datatables instances (feel free to correct me if I am mistaking), it does not look "cross-datatables" friendly and it would require some unnecessary validation/input checking.

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    fnFilter lets you set bRegex parameter (3rd param) true.

    with Regular Expressions you can use the '|' as an "OR" operator.

    http://www.datatables.net/ref#fnFilter
  • filuSfiluS Posts: 4Questions: 0Answers: 0
    edited September 2011
    I know of this, and it doesnt solve the problem. From the way fnFilter works (or at least how I understand it) its filtering some data, either globaly or by one column at the time (one column per one fnFilter call). I need filter to be applied for two rows at the same time because I want it to be used for 2 columns, and that cannot be solved with regular expression.

    EDIT: to make myself clear, when some user puts "random title" into input, it filters out every entry with title containing that string. Lets say it lefts 2 entries after filtration. If user ticks "Include description" checkbox, I want it to search for that string in another column, which means those 2 already found records will stay in table but there can be some more poping up, namely those whose description column contains desired string. This is how I use it:

    applyButton.bind("click", function(event){
    casesDatatableObj.fnFilterClear();
    var textVal = $.trim( text.val() ); //text = jQuery object for filter input
    var includeDescVal = $.trim( includeDescription.is(":checked") );
    if( textVal != 'undefined' && textVal.length > 0 ){
    casesDatatableObj.fnFilter( textVal, 1, true, false );
    if( includeDescVal ){
    casesDatatableObj.fnFilter( textVal, 2, true, false );
    }
    }
    //casesDatatableObj.fnDraw();
    return false;
    });

    I dont think this can be solved with regular expressions.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited September 2011
    you can make 2 fnFilter calls, one for each column with the same filter. all filters remain in effect until you undo them.

    oTable.fnFilter('a|b', 2, true);
    oTable.fnFilter('a|b', 3, true);

    I believe the best way to clear a filter is to just pass in an empty string
    oTable.fnFilter('', 2);
  • filuSfiluS Posts: 4Questions: 0Answers: 0
    I edited previous comment to be clearer of what I am trying to do. With the way fnFilter works, I cant use two sepratate calls that way because first call is always limiting second call (for example, first call filters 5 entries from 40, then second call will be filtering on those 5 items, not 40). I need to find a way how to make both calls to filter data from whole set.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    I see what you mean.

    does oTable.fnFilter('a|b', null, true); work? (filter globally, rather than on specific columns)
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited September 2011
    another option would be to combine your 2 columns into a 3rd column (hidden) and filter on that

    i.e. column 4 = concatenation of column 2 and 3
    fnFilter('a|b', 4, true);
  • filuSfiluS Posts: 4Questions: 0Answers: 0
    fnFilter works nicely on one column, I already tested to be sure :P I am trying to find a solution where I wont do any unnecessary dom manipulation or server calls. But It seems like the best solution would be to go for server-side processing eventhought I am trying to avoid that.
  • allanallan Posts: 63,531Questions: 1Answers: 10,474 Site admin
    You can switch off the global filter's "smart" filtering if you wish and then pass in a regex that you construct yourself, but it sounds more like you might want a custom filter, as described here: http://datatables.net/development/filtering#row_filters . In this way you can build a filter which will work exactly as you wish over the row, with full access to each row available.

    Allan
This discussion has been closed.