Have Filter return no rows when filter string is empty
Have Filter return no rows when filter string is empty
Hi,
I'm trying to modify the Filtering so that no rows are shown whenever the filter string is empty. So, basically I want to invert the default behavior of showing all rows when the filter string is empty. With that, the "Filter" (reducing the set) behaves more like a "Search" (increasing the number of visible rows).
I can "fake" this for the initial loading of the table, by just using "fnFilter()" with a filter string that is not in the table, as in this example:
http://live.datatables.net/aticux/
However, this obviously only works as long as no search string was entered. Once that is manually is removed (so that the text field is empty), all rows are shown again.
Does anybody have ideas on how to achieve this?
Thanks!
Tobias
I'm trying to modify the Filtering so that no rows are shown whenever the filter string is empty. So, basically I want to invert the default behavior of showing all rows when the filter string is empty. With that, the "Filter" (reducing the set) behaves more like a "Search" (increasing the number of visible rows).
I can "fake" this for the initial loading of the table, by just using "fnFilter()" with a filter string that is not in the table, as in this example:
http://live.datatables.net/aticux/
However, this obviously only works as long as no search string was entered. Once that is manually is removed (so that the text field is empty), all rows are shown again.
Does anybody have ideas on how to achieve this?
Thanks!
Tobias
This discussion has been closed.
Replies
[code]
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
return oSettings.oPreviousSearch.sSearch === '' ? false : true;
}
);
[/code]
i.e. when then is no search input, filter out all rows, but when there is an input, allow all rows (since the search is cumulative, the built in filtering will still work).
Live example: http://live.datatables.net/uritig/edit#javascript,html
I should note that this is client-side processing only. Obviously with server-side processing, since the filtering is done at the server, it is up to the server to deal with this.
Allan
awesome! Works perfect, thanks a lot!
Just so that I understood correctly. This adds a callback function to the array of functions that is evaluated for each row, to determine whether that row is matched by the filter. The return values "true" and "false" determine whether the rows will be shown or not. But which callback function gets precedence here? Is that determined by the order of adding them via push()? Or does the first "false" end the evaluation of the filter functions for that array?
Thanks!
Tobias
Spot on. This method means that you could implement entirely custom filtering if you wanted. I've been thinking of doing a 'distance' filtering blog for a time, so it would allow for typos "i.e. `veiw` would match for `view` ).
> Is that determined by the order of adding them via push()?
Again spot on. The filtering in DataTables is cumulative as I say, so it doesn't really matter on the order - if one were to filter it out then it is filtered out and cannot be readded.
Allan