load custom response in datatable using server side processing

别说谁变了你拦得住时间么 提交于 2019-12-24 19:29:45

问题


I am trying to load datatable from ajax response and then perform server-side processing. using this example

this is the response I am receiving from server :

{"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"3215910681","dateTime":"2017-06-05 09:44:22.0","duration":778,"imei":"47350901163665"},{"aNumber":"3224193861","bNumber":"3028540439","dateTime":"2017-04-26 18:53:23.0","duration":266,"imei":"31489802062929"}],"draw":1,"limit":1000,"recordsFiltered":13419,"recordsTotal":13419}

this my javascript code to handle ajax and datatable.

function showDataTable(anumber, startdate, enddate) {
    var cdrReqParams = {};
    cdrReqParams.draw = '1';
    cdrReqParams.offset = 0;
    cdrReqParams.newRequest = '1';
    cdrReqParams.totalRecords = '1';
    cdrReqParams.lookInCol = 'aNumber';
    cdrReqParams.lookInVal = anumber;
    cdrReqParams.fromDate = startdate;
    cdrReqParams.toDate = enddate;

    var jsonStr = JSON.stringify(cdrReqParams);
    console.log(jsonStr);

    API.call("http://localhost:8050/phpservice/json.php", 'POST', function(data) {
        basicData = data.result;
        console.log(basicData);
        oTable = $("#table").dataTable({
            bJQueryUI: true,
            bPaginate: true,
            sPaginationType: "full_numbers",
            bFilter: false,
            bInfo: false,
            bProcessing: true,
            bServerSide: true,
            aaData: [basicData],
            aoColumns: [{
                "sTitle": "ANUMBER",
                "mData": "aNumber"
            }, {
                "sTitle": "BNUMBER",
                "mData": "bNumber"
            }, {
                "sTitle": "DATETIME",
                "mData": "dateTime"
            }, {
                "sTitle": "DURATION",
                "mData": "duration"
            }, {
                "sTitle": "IMEI",
                "mData": "imei"
            }]
        });
    }, function(error) {
        console.log(error);
    }, jsonStr);
}

By doing this, I am receiving 2 errors

DataTables warning: table id=table - Requested unknown parameter 'aNumber' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

and

Invalid JSON Response.

Is there any workaround for this type of problem, In which first, you will perform ajax call and from received data, you will populate datatable with server side processing ??

I hope someone will give me a hint at least.


回答1:


With datatable 1.10,(i think you may be using an earlier version), you can populate and format the data in the cells, by using the columns.data property:

  columns: [             
             { data: "", defaultContent: " " },               

             { data: null, 
               defaultContent: " ", 
               render: function (data, type, row, meta) { 
                                return data.ID; 
               } 
             }
           ]


来源:https://stackoverflow.com/questions/45823994/load-custom-response-in-datatable-using-server-side-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!