来自服务器

时间:2017-01-12 17:10:49

标签: jquery datatables

我正在关注这篇文章

https://datatables.net/examples/data_sources/server_side.html

我有来自服务器的响应,我无法更改,现在我想在数据表中显示该数据。

我试过dataSrc

https://datatables.net/reference/option/ajax.dataSrc

但仍然无法显示数据和计数。 响应格式:

{
  "status":"success",
  "message":{
         "draw": 1,
         "recordsTotal": 57,
         "recordsFiltered": 57,
         "data":[["data1","test","22-05-2016"],["new enrty"
,"newtag,extra","08-01-2017"],["test","kundan","08-01-2017"],["Ram","kundan","08-01-2017"]["ASDA","ASDAS","11-01-2017"],["ASDASD","ASDAS","11-01-2017"]]
         }}

现在我无法在数据表中显示它。

1 个答案:

答案 0 :(得分:1)

你几乎和dataSrc在一起。鉴于您的数据与您所描述的一样,那么这应该有效:

var jsonData = {
    "status": "success",
    "message": {
        "draw": 1,
        "recordsTotal": 57,
        "recordsFiltered": 57,
        "data": [
            ["data1", "test", "22-05-2016"],
            ["new enrty", "newtag,extra", "08-01-2017"],
            ["test", "kundan", "08-01-2017"],
            ["Ram", "kundan", "08-01-2017"],
            ["ASDA", "ASDAS", "11-01-2017"],
            ["ASDASD", "ASDAS", "11-01-2017"]
        ]
    }
};

$(function() {
    var example = $("#example").DataTable({
        //"processing": true,
        "serverSide": true,
        "ajax": {
            "type": 'POST',
            "dataType": 'json',
            "url": '/echo/json/',
            "data": {
                "json": JSON.stringify(jsonData)
            },
            "dataFilter": function(data) {
                var json = jQuery.parseJSON(data);
                return JSON.stringify(json.message);
            }
        },
        columns: [{
            "title": "Column 1"
        }, {
            "title": "Column 2"
        }, {
            "title": "Column 3"
        }]
    });
});

但是,您的数据看起来很像服务器端调用知道DataTables如何工作的东西......例如,它有recordsTotalrecordsFiltered。您是否已经在与知道如何处理DataTables的服务器端进行交谈?

另外,您在原始数据中缺少逗号。工作JSFiddle here。希望有所帮助。

编辑在澄清并进行一些调整之后,这会将消息作为数据。使用dataFilter jQuery ajax回调。答案和JSFiddle更新。