[英]The second execution of jquery datatable dont redraw the data
I'm trying to load data from the server, on the first load it works great, but when I reload, it doesn't do anything.我正在尝试从服务器加载数据,第一次加载时效果很好,但是当我重新加载时,它什么也没做。 Please, don't kill me, this is a legacy project.请不要杀我,这是一个遗留项目。
I try to make a normal call and reload the datatable but didn't work too.我尝试进行正常调用并重新加载数据表,但也没有用。
In the documentation said than the clear of the table will be enoght, I try it and clear function is not reconized.在文档中说,清除表格将是足够的,我尝试了一下并清除 function 没有重新调整。
Script脚本
jQuery(document).ready(function() {
App.initSelect();
//Initialization of the datatable
var grid = new Datatable();
InitGrid(); //function for update the grid;
//Button used to reload the data, and work to go to server
$("#btn_refresh").click(function () {
grid = new Datatable();
InitGrid();
grid.submitFilter(); //if i dont put this, it doesn't go to the server
});
function InitGrid() {
grid.init({
src: $("#datatable_report_sales"),
dataTable: {
"destroy": true,
"serverSide": true,
"bPaginate": false,
"order": [[5, "desc"]],
"ajax": {
"url": "/api/Lotteries/GetSales",
"draw": 1,
"data": function (data) {
data.date = $('#reportrange span').text();
ui.block('Loading...');
},
"dataSrc": function (res) {
ui.unblock();
return res.data;
},
}
}
});
}
});
Html Html
<div class="portlet-body flip-scroll">
<table class="table table-bordered table-striped table-condensed flip-content dataTable no-footer"
id="datatable_report_sales" style="margin: 0 !important;" role="grid">
<thead class="flip-content">
<tr role="row"><th class="sorting" tabindex="0" aria-controls="datatable_report_sales"
rowspan="1" colspan="1" aria-label=" Vendedor : activate to sort column ascending">
Seller </th><th width="15%" class="sorting" tabindex="0" aria-controls="datatable_report_sales"
rowspan="1" colspan="1" aria-label=" Venta : activate to sort column ascending">
Sale </th><th width="15%" class="sorting" tabindex="0" aria-controls="datatable_report_sales"
rowspan="1" colspan="1" aria-label=" Comisión : activate to sort column ascending">
Comisión </th><th width="15%" class="sorting" tabindex="0" aria-controls="datatable_report_sales"
rowspan="1" colspan="1" aria-label=" Comisión Sup. : activate to sort column ascending">
Comisión Sup. </th><th width="15%" class="sorting" tabindex="0" aria-controls="datatable_report_sales"
rowspan="1" colspan="1" aria-label=" Premios : activate to sort column ascending">
Prices </th><th width="15%" class="sorting_desc" tabindex="0" aria-controls="datatable_report_sales"
rowspan="1" colspan="1" aria-sort="descending" aria-label=" Resultado : activate to sort column ascending">
Result
</th></tr>
</thead>
<tbody>
</tbody>
</table>
<div class="bottom"></div><div class="clear"></div>
</div>
I guess than for any reason the destroy need to be forced in this version of datatable, so I destroy the table inside the element table before initializing it again, I'm not sure than this is the most convenient, but it works for now我想无论出于何种原因,都需要在这个版本的数据表中强制销毁,所以我在再次初始化之前销毁元素表中的表,我不确定这是否最方便,但它现在可以工作
$("#btn_refresh").click(function () {
$("#datatable_report_sales").DataTable().destroy();
grid.submitFilter();
grid = new Datatable();
InitGrid();
});
init()
method is only for initializing a DataTable
. init()
方法仅用于初始化DataTable
。 You should use it the first time only.您应该只在第一次使用它。 After you initializing the DataTable, You can use ajax.reload();
初始化 DataTable 后,您可以使用ajax.reload();
to update the table with new data using an ajax request.使用 ajax 请求使用新数据更新表。
https://datatables.net/reference/api/ajax.reload() https://datatables.net/reference/api/ajax.reload()
Try this,尝试这个,
$("#btn_refresh").click(function () {
// ....
grid.ajax.reload();
// ....
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.