fnFilter - switch to "OR" manner
fnFilter - switch to "OR" manner
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.
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.
This discussion has been closed.
Replies
with Regular Expressions you can use the '|' as an "OR" operator.
http://www.datatables.net/ref#fnFilter
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.
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);
does oTable.fnFilter('a|b', null, true); work? (filter globally, rather than on specific columns)
i.e. column 4 = concatenation of column 2 and 3
fnFilter('a|b', 4, true);
Allan