ajax成功后重新加载数据表 [英] reload datatable after ajax success
问题描述
我使用 JQuery 数据表.我在 ajax 成功将数据发送到 json 文件中的数据表 onclick.第一次点击一切都很好,但下一次点击我只得到正确的数据和错误的 dataTables_info 值它总是显示 dataTables_info 的第一个值和分页和行也来自第一个结果.这是datatable中数据的第一次展示:
I use JQuery DataTable. I send data to datatable onclick in json file at ajax succes .the first click everything is good,But the next click I get only the right data ANd wrong value of dataTables_info it display always the first value of dataTables_info And paginatio AND row too from the first result. This is the first display of data in datatable:
所有下一个点击我只得到正确的数据:对于这个例子,它们是显示在下面图片中的一个结果,但其他所有内容(信息、显示、分页)属于第一张图片中显示的第一个搜索:
ALL the next Click I get only right data: For this exemple they are one result showing in picture below but everything else(info ,show,pagination) belong to first search showing in the first picture :
在第二个例子中,当我点击分页的任何页面时,我得到第一页结果的内容!!这是我的 ONclick 函数:
In the second Exemple When I click at any page of pagination I get the content of the first page result!! This is my function ONclick:
$ ( '#ButtonPostJson' ).on('click',function() {
$("tbody").empty();
var forsearch = $("#searchItem").val();
$.ajax({
processing: true,
serverSide: true,
type: 'post',
url: 'searchData.json',
dataType: "json",
data: mysearch,
/* bRetrieve : true,*/
success: function(data) {
$.each(data, function(i, data) {
var body = "<tr>";
body += "<td>" + data.name + "</td>";
..........................
..........................
body += "</tr>";
$('.datatable-ajax-source table').append(body);
})
;
/*DataTables instantiation.*/
$('.datatable-ajax-source table').dataTable();
},
error: function() {
alert('Processus Echoué!');
},
afterSend: function(){
$('.datatable-ajax-source table').dataTable().reload();
/* $('.datatable-ajax-source table').dataTable({bRetrieve : true}).fnDestroy();
$(this).parents().remove();
$('.datatable-ajax-source table').dataTable().clear();*/
}
});
});
我尝试了一切,但我不知道我想念什么.我将这个 jquery 用于数据表:
I try everything and i dont know what I miss. I use this jquery for datatable:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
谢谢.
推荐答案
先获取 table id,比如:
Get table id first, like:
var table=('#tableid').Datatable();
table.draw();
只需将这些行放在 ajax success
函数后即可重新加载数据表
just put these lines after ajax success
function to reload datatable
这篇关于ajax成功后重新加载数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!