{hero}

columns().search()

Since: DataTables 1.10

Search for data in the selected columns.

Description

While search() provides the ability to search globally across the table, this method, and its singular counterpart, provide the ability to search for data on specific columns.

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.

DataTables has a built in search algorithm referred to as "smart" searching and is designed to make searching the table data, easy to use for the end user. The smart search is performed using a regular expression and thus must be considered if you are using a regular expression search (second parameter of this method). For a full description of smart searching in DataTables, please refer to the documentation for search().

Note that this search ability in DataTables is actually technically a filter since it is subtractive. However, we term is a search to avoid 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 columns().search() method's returned object - for example table.columns( [0, 1] ).search( 'Fred' ).draw();. This is to provide the ability to queue multiple changes before performing a draw.

Types

function columns().search()

Description:

Get the currently applied column search.

Returns:

Api instance with the applied search terms for the selected columns in the result set.

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:
Returns:

DataTables API instance

function columns().search( input [, regex[ , smart[ , caseInsen ]]] )

Description:

Set the search term for the columns from the selector. Note this doesn't actually perform the search, but rather queues it up - use draw() to perform the search and display the result.

Parameters:
Returns:

DataTables API instance

Examples

Apply a search to multiple columns:

var table = new DataTable('#myTable');

table
	.columns('.status')
	.search('Important')
	.draw();

Build a text input search for each column. Note that this example performs partial word matching and smart searching.:

// Setup - add a text input to each footer cell
$('#example tfoot th').each(function () {
	var title = $('#example thead th')
		.eq($(this).index())
		.text();
	$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});

// DataTable
var table = new DataTable('#myTable');

// Apply the filter
table.columns().every(function () {
	var column = this;

	$('input', this.footer()).on('keyup change', function () {
		column.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>'));
		});
});

Related

The following options are directly related and may also be useful in your application development.