column().search()

Since: DataTables 1.10

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 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 column().search() method's returned object - for example table.column( [0, 1] ).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 [, regex[ , smart[ , caseInsen ]]] )

Description:

Set the search term for the column 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

Individual column search:

var table = $('#example').DataTable();

// #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 = $('#example').DataTable();

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() )
				.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.