column().search()
Search for data in the selected column.
Description
While search()
provides the ability to search globally across the table, this method, and its plural counterpart, provide the ability to search for data on a specific column.
DataTables does not have any column search controls built-in as there are so many different ways that column specific data could be searched, but this method makes adding custom column search controls super easy. The examples below show how it may be used.
Note that this search ability in DataTables is actually technically a filter since it is subtractive. However, we term it a search to avoid a naming overlap with the filter()
helper method.
Please be aware that this method sets the 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 the column().search()
method's returned object - for example table.column( 0 ).search( 'Fred' ).draw();
. This is to provide the ability to queue multiple changes before performing a draw.
Types
function column().search()
- Description:
Get the currently applied column search.
- Returns:
Search term that is currently applied to the column. This will be an empty string if no search search term is applied.
function column().search( input [, options] )
- Description:
Set the search term to use for the selected column, with configuration options. Call
draw()
to update the table's view once the search term has been setup.- Parameters:
Name Type Optional 1 input
No Search to apply to the column.
As of DataTables 2, this parameter can be given as a regular expression or a function.
If working with a regular expression, the data given is the search data for the cells in column in question.
In the case of a function the function is called for each row with four parameters which are passed in:
- The search string for the column
- The row's original data object
- The row's data index
- The columns's data index
A boolean value should be returned:
true
to keep the row,false
to filter it out.Note that if a regex or function is provided for this parameter, the following parameters are ignored.
2 options
No Configuration options for how the search will be performed. See
DataTables.SearchOptions
for a description of all options. If an option is not given here, the value that it was set to for the previous search will be used.- Returns:
DataTables API instance
function column().search( input [, regex[ , smart[ , caseInsen ]]] )
- Description:
Set the search term for the column from the selector. Note that the signature above is preferred as of DataTables 2, as it provides additional options. This signature is retained for backwards compatibility.
- Parameters:
Name Type Optional 1 input
No The search term to apply. See the
input
parameter in the signature description above for details.2 regex
Yes - default:false Treat as a regular expression (
true
) or not (default,false
).3 smart
Yes - default:true Perform smart search (default,
true
) or not (false
). Note that to perform a "smart" search, DataTables uses regular expressions, so if you pass a regular expression in as the second parameter to this method, you will likely want to disable smart searching so the two different regular expressions don't conflict.4 caseInsen
Yes - default:true Do case-insensitive matching (default,
true
) or not (false
).- Returns:
DataTables API instance
Examples
Individual column search:
var table = new DataTable('#myTable');
// #column3_search is a <input type="text"> element
$('#column3_search').on('keyup', function () {
table
.columns(3)
.search(this.value)
.draw();
});
Build a search for each column with a select-filter
class.:
var table = new DataTable('#myTable');
table.columns('.select-filter').every(function () {
var that = this;
// Create the select list and search operation
var select = $('<select />')
.appendTo(this.footer())
.on('change', function () {
that.search($(this).val(), { exact: true }).draw();
});
// Get the search data for the first column and add to the select list
this.cache('search')
.sort()
.unique()
.each(function (d) {
select.append($('<option value="' + d + '">' + d + '</option>'));
});
});
Individual column search with a function:
let table = new DataTable('#example');
// Find any row with data in column index 3 greater than 50
table
.column(3)
.search((d) => d > 50)
.draw();
Related
The following options are directly related and may also be useful in your application development.