简体   繁体   中英

Getting error on updating datatable with json returned from ajax servlet call

I am getting this eror when I try to load my table using datatable... DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

Here is my jquery call to the servlet

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

由于json是对象数组,因此我将json数组对象的名称修改为aaData,并在ajax列中将其修改为aoColumns和列数据mData ..现在正在工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM