Datatables - prepopulate search box

◇◆丶佛笑我妖孽 提交于 2020-01-04 04:12:10

问题


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:


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



来源:https://stackoverflow.com/questions/28577797/datatables-prepopulate-search-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!