将数据表从外部 JSON 填充到 Datatables 中 - servser 端处理

populate datatable from external JSON into Datatables - servser side processing

提问人:Noob Player 提问时间:8/22/2017 更新时间:8/22/2017 访问量:278

问:

我正在尝试从ajax响应加载datatable,然后执行服务器端处理。

这是我从服务器收到的响应:

{"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"3215910681","cellID":"410-06-325-13123","dateTime":"2017-06-05 09:44:22.0","duration":778,"imei":"47350901163665","imsi":"33372328617146","operatorId":2,"mscId":"3","fileId":"2"},{"aNumber":"3224193861","bNumber":"3028540439","cellID":"410-01-737-38540","dateTime":"2017-04-26 18:53:23.0","duration":266,"imei":"31489802062929","imsi":"34116567506606","operatorId":3,"mscId":"5","fileId":"2"}],"draw":1,"limit":1000,"recordsFiltered":13419,"recordsTotal":13419}

这是我调用 Ajax 的方法,

var cdrReqParams = {};
cdrReqParams.draw = '1'; // need to change with the value from server in next server side ajax call
cdrReqParams.offset = 0; // need to change with the value from server in next server side ajax call
cdrReqParams.newRequest = '1';
cdrReqParams.totalRecords = '1'; // need to change with the value from server in next server side ajax call
cdrReqParams.lookInCol = 'aNumber';
cdrReqParams.lookInVal = anumber;
cdrReqParams.fromDate = startdate;
cdrReqParams.toDate = enddate;

var jsonStr = JSON.stringify(cdrReqParams);
console.log(jsonStr);
API.call("getBasicCallAnalysisData.json", 'POST', function(data) {
    basicData = data.result; // Response from server

}, function(error) {
    console.log(error);
}, jsonStr);

我在basicData中收到服务器响应,如何将该json响应加载到我的数据表中,然后执行进一步的服务器端处理并从服务器更改所需的值(draw,offset,totalRecords),并在单击第2页时执行另一个ajax调用?

以防万一,这是我的 API 类:

var API = {
  call:function(url,type,successcallback,errorCallback,data){
    var data = (!!data) ? data : {};
    var callback = (!!callback) ? callback : function(){};
    $.ajax({
      contentType : "application/json",
      dataType: "json",
      //crossDomain: true,
      xhrFields: { withCredentials: true }, 
      url: url,
      data:data,
      type:type,
      success:successcallback,
      error:errorCallback
    });    
  }
}
JavaScript JSON AJAX 数据表 服务器端

评论

0赞 markpsmith 8/22/2017
您可以更改返回的 json 的格式吗?若要使用服务器端数据源,datatables 需要特定格式的数据。
0赞 Noob Player 8/23/2017
@markpsmith 不,我不能,因为相同的响应也将用于其他模块

答: 暂无答案