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

时间:2014-11-21 06:17:16

标签: javascript jquery html css

当我尝试使用datatable加载我的表时,我收到了这个错误... DataTables警告:table 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>

2 个答案:

答案 0 :(得分:0)

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"}
				]
	});
}

答案 1 :(得分:0)

它现在正在工作..由于json是对象数组,我将json数组对象的名称修改为aaData,将ajax列修改为aoColumns,并将列数据修改为mData ..现在正在运行..非常感谢