Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables - prepopulate search box

I want to be able to store a list of common search terms, which a user can click and thus automatically filter a datatable.

$('#table').on('preXhr.dt', function() {
    alert('test');//$search
})

On http://datatables.net/reference/event/preXhr I saw this event handler could be used to do something before an AJAX call is made (which is perfect - as I could intercept the script at this point, add my prepopulated search term.

But I find that the first time the datatable loads, this event doesn't fire.

Any time I sort, etc it fires before the AJAX call.

But I really need to be able to access the search box in Datatables before the initial dataset is loaded.

How can I do this?

like image 917
b85411 Avatar asked Dec 12 '22 01:12

b85411


1 Answers

You can specify the initial search term in the dataTables options:

$('#table').dataTables({
    search: {
       search: initialSearchTerm
    }
});

where initialSearchTerm is the variable that holds the initial search term.

See the documentation

like image 64
doug65536 Avatar answered Dec 30 '22 09:12

doug65536