I'm trying to initialize a Datatable with a default search value that the user can either replace or refine on. This is with server-side data. I haven't read anything where you can do this in the Datatables documentation.
$('#example_filter label input[type=text]').val('Default Product')
The above sets the value but because there isn't a keypress involved the event handler doesn't pick it up. Is there a method I can chain onto the above that would act like the enter key, or should I write an event handler that looks for changes in the field. I'm pretty new to datatables and a jQuery novice.
So the proper way of doing this is using the oSearch parameter.
https://datatables.net/docs/DataTables/1.9.0/DataTable.defaults.oSearch.html
$(document).ready( function() {
$('#example').dataTable( {
"oSearch": {"sSearch": "Initial search"}
} );
} )
You can trigger an event manually with .trigger()
:
$('#example_filter label input[type=text]')
.val('Default Product')
.trigger($.Event("keypress", { keyCode: 13 }));
Depending on your code, you may want "keyup"
instead.
The proper way is now:
var table = $( '#mytable' ).DataTable();
table.search( 'initial search value' ).draw();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With