Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value entered in search box In Datatables

How to get value entered in search box In Datatables. Is there a way to get value entered in search box in Data Tables?

like image 892
jessi Avatar asked Oct 27 '14 15:10

jessi


2 Answers

If you just want to check the value when a search is performed [dataTables 1.10.x] :

var table = $('#example').DataTable();  $('#example').on('search.dt', function() {     var value = $('.dataTables_filter input').val();     console.log(value); // <-- the value });  

if you want to check the value before the search, and be able to cancel the search, you must unbind the default searchbox event and create your own, like this - search only when the user has entered more than 3 characters :

$('.dataTables_filter input').unbind().keyup(function() {     var value = $(this).val();     if (value.length>3) {         table.search(value).draw();     }  }); 

demo -> http://jsfiddle.net/pb0632c3/

To reset the search / filter completely, like if the user has deleted the search term :

if (value.length==0) table.search('').draw(); 
like image 70
davidkonrad Avatar answered Sep 21 '22 16:09

davidkonrad


Yes, there is. You call the search method without an argument to get the search term

var query = dataTable.search()

https://datatables.net/reference/api/search()

Get the currently applied global search. If there is more than one table in the API's context, the search term of the first table will be returned. Use table() if you require the search term of a different table in the API's context.

like image 41
Peter Chaula Avatar answered Sep 21 '22 16:09

Peter Chaula