2

I'm trying to filter a datatable by setting the search input value to what I want to find:

document.querySelector("#searchinput").value = $whatiwant;

The value is being set successfully, but it doesn't affect the datatable results. I tried to submit it (but it isn't a form, so it did't work) and to simulate the enter key press (but it didn't work either and didn't return any errors). I also tried to use the DataTables filter function, but nothing seems to happen.

After all, what can I do to search/filter a datatable?

DoceAzedo
  • 342
  • 4
  • 12

2 Answers2

2

You can use Datatables API search()

The global search is performed across all searchable columns

link: search()

Example snippet code:

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

// #myInput is a <input type="text"> element
$('#myInput').on( 'keyup', function () {
    table.search( this.value ).draw();
} );

Or use API column().search() for specific column

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.

link: 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();
} );
mike85
  • 621
  • 4
  • 8
0

Did you set searching:false in datatable options? you can set searching enable and disable from that option.

datatables searching

You can enable searching by coding as follows. by doing this you don't need to add a separate input to search in datatable.

$('#example').dataTable( {
  "searching": true
} );
Lahiru Madusanka
  • 270
  • 2
  • 13