Custom search in DataTable
Custom search in DataTable
Hello
DataTable provides search on table data, but it not works with input and drop-downs cells data in table. My tables contains text, inputs and dropdowns cells and I need to make search on all columns data, not only text. I found few approaches could help on that, but chosen one of them to implement. This is the Search Plugin https://datatables.net/manual/plug-ins/search. I did it, but found the problem. The default all row search, applied before my custom Search Plugin. This leads to, the rows actually should be visible in table, but they are already filtered with default search. I see this
done within _fnFilterComplete method in jqueryDataTables.js. Here the code abstract:
/* Global filter */
_fnFilter( oSettings, oInput.sSearch, iForce, fnRegex(oInput), oInput.bSmart, oInput.bCaseInsensitive );
fnSaveFilter( oInput );
/* Now do the individual column filter */
for ( var i=0 ; i<aoPrevSearch.length ; i++ )
{
_fnFilterColumn( oSettings, aoPrevSearch[i].sSearch, i, fnRegex(aoPrevSearch[i]),
aoPrevSearch[i].bSmart, aoPrevSearch[i].bCaseInsensitive );
}
/* Custom filtering */
_fnFilterCustom( oSettings );
So next thing i tried to find how to disable default search "Global filter", but not find any flag in source code or manual. Just one idea I tried described here https://datatables.net/development/filtering:
if you wish to completely disable DataTables' own global filtering and replace it with your own, just drop the 'f' (filtering) option from sDom.
Not sure at all is this applicable because of cannot get when search and filter means the same and when are different, but I tried it. Though it actually deletes the search box from the table DOM.
So the question is how to disable default search calls like:
_fnFilter( oSettings, oInput.sSearch, iForce, fnRegex(oInput), oInput.bSmart, oInput.bCaseInsensitive );
fnSaveFilter( oInput );
for ( var i=0 ; i<aoPrevSearch.length ; i++ )
{
_fnFilterColumn( oSettings, aoPrevSearch[i].sSearch, i, fnRegex(aoPrevSearch[i]),
aoPrevSearch[i].bSmart, aoPrevSearch[i].bCaseInsensitive );
}
but leave
_fnFilterCustom( oSettings )
custom Search plugin call working, so search will only work with custom Search Plugin? Or could it be that I'm just do search in wrong way and exist some other method to search an all columns data even if cell contain inputs or drop down lists?
This question has an accepted answers - jump to answer
Answers
Thanks for your post.
The search is cumulative, so it shouldn't really matter if the custom search is done before or after the global search. If a row is excluded by one, it will be excluded regardless of order.
Just remove the
f
option from thedom
property.Allan
Thanks for your help. Your answered my initial question. Though here my ideas about how it could be improved:
Lets consider table row with two columns, where the second one is drop-down.
The "f" option present in Dom property. I did Search plugin, and I check cell type in it:
When I type "r" in search input I will see line 1 disappear (as others). I think this because default search will filter out the row before it have chance to be checked in custom filter. Right?
When I type "a" I will get just the row 3 visible. It means that in line 2 first column "Pit" was filtered by default search before row was checked in custom Search plugin ("Goal" contains "a" so this line should be found).
So yes, custom Search plugin will work on lines, that was previously found by default search. This looks more like filtering but not search. So when I type search text the search input, the rule is:
((Default search == true) AND (Custom search == true)) then found
But I would like:
((Default search == true) OR (Custom search == true)) then found
Is this possible?
If this not possible, I have to turn off default search and add to my cusom Search plugin condition to process text cells too:
As about "f" option. It also hides default search input box from page, so I have to create new search input and add event listener to it. Though just turn off default search method call would be enough. So the default search input event listener will call custom Search only.
Correct. However, even if the order were reversed and the built in filter ran after the custom filter, that first row would still get filtered out.
Thanks for the clarification - I understand now. No, if you are using the built in filter at all, then that is not possible. You would have to use entirely custom filtering to allow that to work.
Allan
Thanks a lot!