gpt4 book ai didi

javascript - 使用从 ajax servlet 调用返回的 json 更新数据表时出错

转载 作者:行者123 更新时间:2023-11-28 08:58:05 24 4
gpt4 key购买 nike

当我尝试使用数据表加载我的表时出现此错误... DataTables 警告:表 id=example - 无法重新初始化 DataTable。有关此错误的更多信息,请参阅 http://datatables.net/tn/3

这是我对 servlet 的 jquery 调用

function fetchClients(id){

$('document').ready(function(){
$('#example').dataTable({
"processing":true,
"serverSide":true,
"ajax":{
"url":"/DashBoard/FetchClients",
"dataType":"json",
"data": {alertId: id},
"type":"POST"
},
"columns":[
{"data": "obj.clientName"},
{"data": "obj.AliasName"},
{"data": "obj.Status"},
{"data": "obj.Time"},
{"data": "obj.Date"}
]
});
});
}

while(rs.next()){
obj1=new JSONObject();
obj1.put("clientId",rs.getString("ClientID"));
obj1.put("clientName",rs.getString("ClientName"));
obj1.put("AliasName",rs.getString("AliasName"));
obj1.put("Status", rs.getString("Status"));
Timestamp tem=rs.getTimestamp("Date");
SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d1=s1.parse(tem+"");
int hour=d1.getHours();
int min=d1.getMinutes();
if(hour==0){
obj1.put("Time", hour+":"+min+"PM");
}
else if(hour>=13)
{
obj1.put("Time", (hour-12)+":"+min+"PM");
}
else
obj1.put("Time", hour+":"+min+"AM");
s1=new SimpleDateFormat("dd/MM/yyyy");
obj1.put("Date", s1.format(d1)+"");
obj.put(obj1);
}
json.put("obj", obj);
}
rs.close();
stmt.close();
conn.close();
out.print(json);
out.close();
<table id="example" class="display" cellspacing="0" width="100%">

</table>

最佳答案

var table; // global variable, also can skip this statement
function fetchClients(id){
if(table){ table.destroy(); } //destroy if already initialised
table = $('#example').dataTable({
"processing":true,
"serverSide":true,
"ajax":{
"url":"/DashBoard/FetchClients",
"dataType":"json",
"data": {alertId: id},
"type":"POST"
},
"columns":[
{"data": "obj.clientName"},
{"data": "obj.AliasName"},
{"data": "obj.Status"},
{"data": "obj.Time"},
{"data": "obj.Date"}
]
});
}

关于javascript - 使用从 ajax servlet 调用返回的 json 更新数据表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27055273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com