Sarva Praharanayuthan

One who uses everything as his weapon!

Set initial search value for datatables with AJAX

To setup initial search value of datatables, as per the documentation we can use,

search:
{
    search: "Search text"
}

However, when it is a AJAX table, this will not work. To make it work, we will have to modify our code as,

let searchText = "Search text";
let table = $('#table').on('preXhr.dt', function(e, settings, data)
{
// Set the initial search value
    if (!settings._searchSet)
    {
        data.search.value = searchText;
        settings._searchSet = true;
    }
}).DataTable({
    ajax: 'https://www.example.com/fetch-result',
    serverSide: true,
    processing: true
});

 

Set initial search value for datatables with AJAX
Scroll to top