Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Datatables. Set default search value for a column when initializing a table

I'm using datatable 1.10.15 with server side processing. I want to set value for a column search when table initializes. I tried this but without success:

$('#dataTable').DataTable({
    ...
    columns:[
        ....
        {name:'name', search:{value:'q'}}
        ....
    ]
});
like image 603
Andriy Lozynskiy Avatar asked Jan 18 '18 07:01

Andriy Lozynskiy


2 Answers

You can try:

$('#example').dataTable( {
  "search": {
    "search": "Fred"
  }
});

It will initialize the datatable with Fred in search column.

Working Fiddle

Reference

like image 57
Mayank Pandeyz Avatar answered Sep 22 '22 00:09

Mayank Pandeyz


Here can be used SearchCols.

Basically the same as the search option, but in this case for individual columns, rather than the global filter, this option defined the filtering to apply to the table during initialisation.

The array must be of the same size as the number of columns, and each element be an object with the parameters search and escapeRegex (the latter is optional). 'null' is also accepted and the default will be used.

Example:

$('#example').dataTable( {
  "searchCols": [
    null,
    { "search": "My filter" },
    null,
    { "search": "^[0-9]", "escapeRegex": false }
  ]
} );

This is the only right solution when you can set a default search value when initializing a datatable. it's worth the datatable documentation.

More info: https://datatables.net/reference/option/searchCols

Working example: http://live.datatables.net/piqidoqo/603/edit

like image 28
NKSM Avatar answered Sep 22 '22 00:09

NKSM