Datatables - prepopulate search box

2019-07-21 14:55发布

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?

1条回答
狗以群分
2楼-- · 2019-07-21 15:48

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

查看更多
登录 后发表回答