[英]jQuery Datatables populate search data
On document.ready, my Datatable loads accordingly. 在document.ready上,我的数据表会相应加载。
What I need to do is build a feature that reloads the Datatable if the user conducts a search. 我需要做的是构建一个功能,如果用户进行搜索,该功能将重新加载数据表。
So the Datatable loads like this: 因此,数据表的加载如下:
$(document).ready(function()
{
$('#searchSubmit').on('click', function() // used for searching
{
var searchbooking = $('#searchbooking').val();
var searchquote = $('#searchquote').val();
var searchtli = $('#searchtli').val();
if(searchbooking == "" && searchquote == "" && searchtli == "")
{
$('.message').text('You did not enter any search criteria.');
return false; // making sure they enter something
}
else
{
$.post('api/searchAll.php', {searchbooking:searchbooking, searchquote:searchquote, searchtli:searchtli}, function(data)
{
// what do i do here???
// how do I get the return results to load
});
}
});
// if the user does not enter any search parameters, load everything
$('#example1').DataTable({
"ajax": {
"url": "api/displayQnams.php",
"type": "POST",
"dataSrc": ''
},
"columns": [
{ "data": "" },
{ "data": "column1" },
{ "data": "column2" },
{ "data": "column3" }
],
"iDisplayLength": 25,
"order": [[ 6, "desc" ]],
// and so on
});
});
As you will see in the above code, when the document is ready, if the user does not conduct a search, I load all of the data from the process called 'displayQnams.php'. 正如您将在上面的代码中看到的那样,当文档准备就绪时,如果用户未进行搜索,那么我将从名为“ displayQnams.php”的进程中加载所有数据。
But if the user conducts a search, the parameters are sent to another process called 'qnamsSearch.php'. 但是,如果用户进行搜索,则将参数发送到另一个名为“ qnamsSearch.php”的进程。
How do I reload the datatable with the search results from 'qnamsSearch.php'? 如何使用'qnamsSearch.php'的搜索结果重新加载数据表?
I tried to create a variable from inside the post: 我试图从帖子内部创建一个变量:
var dataUrl = data;
And I tried to call that variable in the ajax call: 我试图在ajax调用中调用该变量:
"ajax": {
"url": dataUrl,
"type": "POST",
"dataSrc": ''
}
But the Datatable will not display anything and there are no console errors. 但是,数据表将不显示任何内容,也没有控制台错误。
How can I make this work? 我该如何进行这项工作?
You can try using this. 您可以尝试使用此功能。
After user click search button, below is the flow: 用户单击搜索按钮后,以下是流程:
Redraw back datatable with new data - datatables draw() 使用新数据重新绘制数据表-数据表draw()
$(document).ready(function(){ var datatable = $('#example1').DataTable({ "ajax": { "url": "api/displayQnams.php", "type": "POST", "dataSrc": '' }, "columns": [ { "data": "" }, { "data": "column1" }, { "data": "column2" }, { "data": "column3" } ], "iDisplayLength": 25, "order": [[ 6, "desc" ]] }); $('#searchSubmit').on('click', function(){ var searchbooking = $('#searchbooking').val(); var searchquote = $('#searchquote').val(); var searchtli = $('#searchtli').val(); if(searchbooking == "" && searchquote == "" && searchtli == ""){ $('.message').text('You did not enter any search criteria.'); return false; // making sure they enter something } else { $.post( 'api/searchAll.php', { searchbooking:searchbooking, searchquote:searchquote, searchtli:searchtli }, function(data) { var newData = data; datatable.clear().draw(); datatable.rows.add(newData); // Add new data datatable.columns.adjust().draw(); // Redraw the DataTable }); } }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.