javascript - load datatable from custom response -
i working on datatables. have posted 2 questions related question this , this.
ajax call:
api.call("getbasiccallanalysisdata.json", 'post', function(data) { basicdata = data; createtable(basicdata); }, function(error) { console.log(error); }, jsonstr);
this json response server:
{"msg":null,"code":null,"status":null,"result":[{"anumber":"3224193861","bnumber":"3217284244","cellid":"410-01-604-30226","datetime":"2017-06-24 23:08:09.0","duration":801,"imei":"27512671195807","imsi":"35788979525680","operatorid":1,"mscid":"2","fileid":"1"},{"anumber":"3224193861","bnumber":"3217280585","cellid":"410-01-738-13433","datetime":"2017-06-24 06:53:13.0","duration":198,"imei":"46341570864238","imsi":"33270572168878","operatorid":2,"mscid":"4","fileid":"2"}],"draw":1,"limit":1000,"recordsfiltered":13442,"recordstotal":13442}
html table :
<table id="table" class="display responsive nowrap" cellspacing="0" width="100%"> <thead style="background-color:#303641;"> <tr> <th>anumber</th> <th>bnumber</th> <th>datetime</th> <th>duration</th> <th>imei</th> <th>imsi</th> <th>cellid</th> <th>oprid</th> <th>msc id</th> <th>file id</th> </tr> </thead> </table>
and function load datatable facing 2 errors:
function createtable(data) { console.log(data); $('#table').datatable({ "processing": true, "serverside": true, "bfilter": false, "idisplaylength": configdata, dom: 'bfrtip', buttons: ['colvis', 'print', 'csv', 'excel', 'pdf'], searching: false, language: { buttons: { colvis: 'show/hide columns' } }, "aadata": data }); }
the parameter data
response server. errors :
datatables warning: table id=table - requested unknown parameter 'anumber' row 0, column 0. more information error, please see http://datatables.net/tn/4
and
invalid json response.
any idea, how can populate datatable json response ?
there 2 possibility json datatables...
you use first one.
there 2 main problems code:
- a superflous datatables attribute.
- a valid json, not structured datatables expects it.
#1
attribute "serverside": true,
says intend use "remote processing". datatables expects "ajax" attribute holding php address json. missing attribute triggered "invalid json" error.
i hear shouting loud unclear error messages now!!! (lol)
#2
datatables expects data structured array whole table...
array has hold arrays of "row" values. example here.
that not have.
made function "re-arrange" data satisfy datatables expectations.
console.clear(); // function run trhough json , restructure data array of array. function arrangedata(data){ var datatabledata = []; for(i=0;i<data.length;i++){ var row = [ data[i].anumber, data[i].bnumber, data[i].cellid, data[i].datetime, data[i].duration, data[i].imei, data[i].imsi, data[i].operatorid, data[i].mscid, data[i].fileid ]; datatabledata.push(row); } console.log(datatabledata); return datatabledata; } // original function not changed... function createtable(data) { $('#table').datatable({ "processing": true, //"serverside": true, // works "remote preocessing" ajax attribute. "bfilter": false, //"idisplaylength": configdata, // commented 1 because did not have "configdata" variable. dom: 'bfrtip', buttons: ['colvis', 'print', 'csv', 'excel', 'pdf'], searching: false, language: { buttons: { colvis: 'show/hide columns' } }, "aadata": arrangedata(data) // i'm calling "arrange" function here. }); } // simulate response form jquery ajax. var dataset = { msg:null, code:null, status:null, result:[ { "anumber":"3224193861", "bnumber":"3217284244", "cellid":"410-01-604-30226", "datetime":"2017-06-24 23:08:09.0", "duration":801, "imei":"27512671195807", "imsi":"35788979525680", "operatorid":1, "mscid":"2", "fileid":"1" }, { "anumber":"3224193861", "bnumber":"3217280585", "cellid":"410-01-738-13433", "datetime":"2017-06-24 06:53:13.0", "duration":198, "imei":"46341570864238", "imsi":"33270572168878", "operatorid":2, "mscid":"4", "fileid":"2" } ], "draw":1, "limit":1000, "recordsfiltered":13442, "recordstotal":13442 }; // call table draw function... in ajax success callback. createtable(dataset.result);
wiki
Comments
Post a Comment