我正在用1个ajax调用填充2个数据表。那是包裹在jQuery.ajax中的2个数据表填充。因此,我必须使用DataTable().clear().rows.add
进行更新。这是初始人口的样子:
pojQuery(function(){
jQuery.ajax({
url: '/api/?action=getStats',
type: 'GET',
dataType: 'json',
success: function(data) {
if(data.data.instances[0].Strategy != "None yet"){
anyInstance = true;
tableInterval = 4000;
}
console.log(data);
jQuery("#tablepress-1").DataTable({
"bProcessing": true,
//url:'/api/?action=getStats',
data: data.data.instances,
dataSrc: 'data',
"deferRender": true,
"columns": [
{ "data": "Item 1" },
{ "data": "Item 2" },
{ "data": "Item 3" },
{ "data": "Item 4" },
{ "data": "Item 5" },
//{ "data": "ID" }
],
"aoColumnDefs": [
{
"aTargets": [5],
"mData": "ID",
"mRender": function (data, type, full) {
if(anyInstance){
return '<a href="javascript:void(0)" onclick="return false;"><i class="fas fa-trash delIns" value="' + data + '"></i></a>';
}else{
return '...';
}
}
}
]
})
//var header1 = "<th>Item 1</th><th>Item 2</th><th>Item 3</th><th>Item 4</th><th>Item 5</th>";
//jQuery("#tablepress-1").append(header1);
var instanceTable = jQuery('#tablepress-1 th');
instanceTable.eq(0).text( "Item 1" );
instanceTable.eq(1).text( "Item 2" );
instanceTable.eq(2).text( "Item 3" );
instanceTable.eq(3).text( "Item 4" );
instanceTable.eq(4).text( "Item 5" );
jQuery("#tablepress-2").DataTable({
"bProcessing": true,
//url:'/api/?action=getUserTrades',
data: data.data.trades,
dataSrc: 'data',
"deferRender": true,
"columns": [
{ "data": "Item 1" },
{ "data": "Item 2" },
{ "data": "Item 3" },
{ "data": "Item 4" },
{ "data": "Item 5" },
{ "data": "Item 6" },
{ "data": "Item 7" }
]
})
var tradesTable = jQuery('#tablepress-2 th');
tradesTable.eq(0).text( "Item 1" );
tradesTable.eq(1).text( "Item 2" );
tradesTable.eq(2).text( "Item 3" );
tradesTable.eq(3).text( "Item 4" );
tradesTable.eq(4).text( "Item 5" );
tradesTable.eq(5).text( "Item 6" );
tradesTable.eq(6).text( "Item 7" );
updateWidgets(data)
}
});
})
(我更改了一些“隐私”值)
如您所见,我正在使用aoColumnDefs
。这就是我显示删除行的操作图标的方式。这是我定期更新数据的方法:
function updateTables(){
jQuery.ajax({
url: '/api/?action=getStats',
type: 'GET',
dataType: 'json',
success: function (data) {
jQuery('#tablepress-1').DataTable().clear().rows.add(data.data.instances).draw();
jQuery('#tablepress-2').DataTable().clear().rows.add(data.data.trades).draw();
updateWidgets(data)
}
})
}
我在计时器和其他任何需要的地方调用它。问题是,我用aoColumnDefs
填充的操作列未在其中定义。因此,当表更新时,除该行外,我所有的行均更新。怎么做?