Replace search function entirely, not just $.fn.dataTable.ext.search.push
Replace search function entirely, not just $.fn.dataTable.ext.search.push
We have set the searching parameter to true and it works as expected.
However we want to cusomise the search function, depending on what is entered we would like to show and hide different rows and it is a bit more advanced than just searching the data for a string.
Now we have seen the $.fn.dataTable.ext.search.push function. It adds additional filtering. We would however like to replace the entire filtering function if possible so that we can get full control.
Is it possible to keep the search field, but replace the function used for filtering?
Answers
You can do some advanced searches with the search plugin ($.fn.dataTable.ext.search.push). More than just strings. It does augment the builtin search functionality. In order to completely replace the search functionality you would need to modify the datatables.js file you are using. If that is something you would to do then @allan or @colin can guide you one what code to replace. However, I don't think that would be a good plan as it will be problematic to perform upgrades.
Short of actually writing your own search function I suggest you develop a search plugin and eliminate the use of the Datatable search API - meaning remove the default search input and create your own. Then with your input event handler simply use
draw()
to draw the table. This will perform any Datatables searches whcih you won't have then execute the search plugins.Here is a simple example using checkbox searches:
http://live.datatables.net/wenaxuti/1/edit
Kevin
Thanks for the reply @kthorngren. I would love if there would be a setting for disabling the built in search.
I am not keen at changing the source code of the library, feels hard to maintain. I tihnk however that making it possible ti disable the built in search method would be something very useful as this is not the first time I have got this need.
A suggestion @allan: Could the default search function be put inside the $.fn.dataTable.ext.search array? If so I could actually do:
$.fn.dataTable.ext.search.pop()
and then
$.fn.dataTable.ext.search.push() our own function
or maybe even $.fn.dataTable.ext.search = [ our_search_function ]
?
Kind regards
Jens
I don't think you will be able to disable the builtin search functionality nor pop it off the $.fn.dataTable.ext.search array. You can use
dom
to remove the default search input. Then make sure you don't usesearch()
,column().search()
orcolumns().search()
to search Datatables. Doing this you will effectively eliminate the builtin search functionality.If you want help with the search plugin please describe what you are trying to do?
Kevin
I tried now creating my own search field, but if I remove the standard search field by entering
searching: false
in datatables constructor, then my custom filter handler added by $.fn.dataTable.ext.search.push() is never called.
I can see that. There would be no reason to run the search function if you disable it. Instead of using
searching: false
try using thedom
option and leave out thef
.Kevin
This is a valid request. If you want to customize the search behaviour of the search input field, you need to be able to replace the built-in search function.
For example, you want to implement include and exclude assertions for space-delimited terms. Entering
"+term1 -term2"
should perform"include(term1) and exclude(term2)"
. If you push a custom search plugin, it will never be called, because the built-in search function will use the search string top match the data and always returnfalse
earlier.It is possible to place a custom input field to the page and filter the table by its API, but it is too much work. There is a perfectly usable input field for searching in the DataTable. The simplest way how to customize the search woudl be to just replace the built-in function, which just matches strings and returns
true
orfalse
.This example disables the Datatables event handler for the default search input and creates its own. It uses
draw()
to draw the table which will invoke the search plugins.http://live.datatables.net/yisaqore/1/edit
This example will always display rows with
Edinburgh
in the Office column plus searches for the the columns for the search input. Its not a complete example but gives an idea of how to bypass the Datatables search engine. However you can still use the search API's if desired.Kevin
Thank you, Kevin, for the cunning idea to unsubscribe the DataTables input listener! It is still not as robust as a search callback on the interface, because DataTables could start listening on the
input
event, for example, but it avoids patching DataTables and will probably survive unbroken for a long time.In the meanwhile, I patched
_fnFilter
to call afunction (string, terms)
, which could be an inspiration for a public API likefunction (settings, searchData, input) : bool
.