Initialize Search in jQuery Datatables

I am trying to initialize a Datatable with a default search value that the user can either replace or refine. This is with server side data. I have not read anything where you can do this in the Datatables documentation.

$('#example_filter label input[type=text]').val('Default Product') 

The above value sets the value, but since the keystroke is not enabled, the event handler does not accept it. Is there a method that I can associate with the above that will act as an input key, or should I write an event handler that looks for changes to the field. I am new to datatables and new to jQuery.

+10
source share
6 answers

So, the right way to do this is to use the oSearch parameter.

https://datatables.net/docs/DataTables/1.9.0/DataTable.defaults.oSearch.html

 $(document).ready( function() { $('#example').dataTable( { "oSearch": {"sSearch": "Initial search"} } ); } ) 
+21
source

You can trigger the event manually using .trigger() :

 $('#example_filter label input[type=text]') .val('Default Product') .trigger($.Event("keypress", { keyCode: 13 })); 

Depending on your code, you may need a "keyup" .

+13
source

The right way:

 var table = $( '#mytable' ).DataTable(); table.search( 'initial search value' ).draw(); 
+11
source

oSearch related oSearch use outdated syntax. As in DataTables 1.10+ , the correct syntax is:

 $(document).ready( function() { $('#example').dataTable( { "search": {"search": "Initial search"} }); }); 
+3
source

You can change the default settings:

 var my_config = { oLanguage: { sSearch: "" }, oSearch: { sSearch: "Default Search value" } }; $('#search').dataTable(my_config); 
0
source
 $('#example_filter label input[type=search]').val(i).trigger($.Event("keyup", { keyCode: 13 })); 
0
source

Source: https://habr.com/ru/post/917920/


All Articles