最近項目使用datatable,但是刷新遇到了問題,試過fnDraw等函數沒有反應,datatable官方api是用ajxa.reload,但是會報沒有定義ajax。
還有這個api解釋中:
var table = $('#example').DataTable( {
ajax: "data.json"
} );
setInterval( function () {
table.ajax.reload();
}, 30000 );
data.json我不太明白是哪個數據,是重新刷新要賦給的數據?,那要怎麼傳進去呢
下面貼一下我的代碼:
jQuery.ajaxSetup({ headers: { Authorization: 'MDphZG1pbg=='} });
jQuery(function($) {
oTable1 = $('#sample-table-2').DataTable( {
/*"columnDefs": [{
"defaultContent": "-",
"targets": "_all"
}],*/
"sAjaxDataProp":"data",
ajax: aa,
/*"sPaginationType": "full_numbers",*/
/*"sAjaxSource":"http://123.56.93.33:8088/kiunion-data-provider/ws/users/1",*/
"sAjaxSource":"http://123.56.93.33:8088/kiunionSer/ws/v1.0/vehicleBaseInfo/searchByEnterprise?enterpriseId=1",
"bProcessing" : true,
"bServerSide": true,
"bAutoWidth":true,
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success":fnCallback,
} );
"aoColumns": [
{ mData: 'VehicleID' },
{ mData: 'VehicleID' },
{ mData: 'StatusID' },
{ mData: 'PlateNumber' },
{ mData: 'SeqNumber' },
{ mData: 'TeamID' },
{ mData: 'VehicleID' }
],
"aoColumnDefs": [
{
"aTargets": [0],
"mRender": function(data, type, full) {
return "<td><label><input type='checkbox'class='ace' /><span class='lbl'></span></label></td>";
}
},
],
"aLengthMenu": [10,20,30,40,50, 100],
"oLanguage": {
"sLengthMenu": "每頁顯示 _MENU_ 條記錄",
"sZeroRecords": "抱歉, 沒有找到",
"sInfo": "從 _START_ 到 _END_ /共 _TOTAL_ 條數據",
"sInfoEmpty": "沒有數據",
"sInfoFiltered": "(從 _MAX_ 條數據中檢索)",
"oPaginate": {
"sFirst": "首頁",
"sPrevious": "前一頁",
"sNext": "後一頁",
"sLast": "尾頁"
},
"sZeroRecords": "沒有檢索到數據",
"sProcessing": "<img src=’./loading.gif’ />"
}
} );
$("#acecom").on('click',function(){
console.log("reload");
oTable1.ajax.reload();
})
})
table.ajax.reload();是1.10+版本的api,你的位置是1.9-的,這2個是分水嶺版本。
1.9+用 oTable1.fnDraw()來更新