search.fixed()
Get / set a named search to apply to a table.
Description
This method provides the ability to apply a "fixed" search to a DataTable. They might also be termed sticky or named searches. The primary difference from a standard search term is that a fixed search is not immediately replaced by the next search term applied. It is consistently applied to the table, until either replaced by name or is deleted.
Fixed search terms are particularly useful when you want to layer searches - i.e. add multiple search terms and have the table display the cumulative result.
This interface is usually expected to be used by developers creating a custom search interface for DataTables.
Please be aware that this method sets the fixed search to apply to the table only - it does not actually perform the search. In order to have the search performed and the result shown, use the draw()
method, which can be called simply as a chained method of this method.
When using server-side processing, additional logic must be added at the server-side to handle fixed search terms. Additionally, searching by functions is not possible, since the search function is client-side based.
Types
function search.fixed()
- Description:
Get a list of the names of searches applied to the table.
- Returns:
An API instance that contains the names of the search terms applied to the table.
function search.fixed( name )
- Description:
Get the search term used for the given name.
- Parameters:
Name Type Optional 1 name
No Fixed search term to get.
- Returns:
The search term that was originally applied.
function search.fixed( name, searchTerm )
- Description:
Set a search term to apply to the table, using a name to uniquely identify it.
- Parameters:
Name Type Optional 1 name
No Name to give to the search term so it can be addressed in future.
2 input
No The search term to apply to the table.
Use
null
for this value to delete an existing search term.If given as a string, it will be applied to the table's search using DataTables "smart" search (see
search()
).If working with a regular expression, be aware that the regex is applied to a single string with the search data from all searchable columns (double space joined).
In the case of a function the function is called for each row with three parameters which are passed in:
- The search string of data from all searchable columns in the table
- The row's original data object
- The row's data index
A boolean value should be returned:
true
to keep the row,false
to filter it out.- Returns:
DataTables API instance
Examples
Get an array of current search names:
let table = new DataTable('#myTable');
let searches = table.search.fixed().toArray();
Get an expecting search term:
let search = table.search.fixed('mySearch');
Applying a search string:
table.search.fixed('myString', 'Paris');
Applying a search regex:
table.search.fixed('myRegex', /l.*n/i);
Applying a search function:
table.search.fixed('myFunc', (row, data) => {
return row.includes('New York');
});
Deleting an existing search term:
table.search.fixed('mySearch', null);
Related
The following options are directly related and may also be useful in your application development.