如何为 jQuery DataTables 获取 JSON 的某些部分

Posted

技术标签:

【中文标题】如何为 jQuery DataTables 获取 JSON 的某些部分【英文标题】:How to get some part of JSON for jQuery DataTables 【发布时间】:2015-07-31 07:36:31 【问题描述】:

我在 jQuery DataTables aaData 属性中使用 webapi,如下所示:

 "aaData": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",

此请求返回一个 JSON,如下所示:


"return":
    "status":200,"message":"Accept",
"entries":
[
    "messageid":40859622,"message":"Text1","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146,
    "messageid":40859621,"message":"Text2","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146,
    "messageid":40859589,"message":"Text3","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868620,"cost":146,
    "messageid":40858860,"message":"Text4","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867960,"cost":146,
    "messageid":40858483,"message":"Text5","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867600,"cost":146
]

但我只需要将“条目”的行传递给我的aaData,如下所示:

    "messageid":40859622,"message":"Text1","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146,
    "messageid":40859621,"message":"Text2","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146,
    "messageid":40859589,"message":"Text3","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868620,"cost":146,
    "messageid":40858860,"message":"Text4","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867960,"cost":146,
    "messageid":40858483,"message":"Text5","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867600,"cost":146

我该如何工作

"aaData": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",

获取我的必要值?

【问题讨论】:

假设您将其存储在 var data = ... 中,然后是 data['entries'] 我解释了我的问题,请帮助如何更改 url 以获得我的必要值? 【参考方案1】:

您需要使用sAjaxDataProp (DataTables 1.9) 或ajax.dataSrc (DataTables 1.10),以下是手册的摘录:

默认情况下,DataTables 会在以下情况下查找属性“aaData” 从 Ajax 源获取数据或用于服务器端处理 - 此参数允许更改该属性。您可以使用 javascript点对象表示法获取多个数据源 嵌套级别。

以下是不同DataTables版本的正确初始化代码。

数据表 1.9.x

$(document).ready(function() 
  var oTable = $('#example').dataTable(
    "sAjaxSource": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",
    "sAjaxDataProp": "entries"
  );
);

数据表 1.10.x

$(document).ready(function() 
  var oTable = $('#example').dataTable(
    "ajax": 
        "url": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",
        "dataSrc": "entries"
    
  );
);

【讨论】:

【参考方案2】:
function AssignResult() 

    //Call Web API
    var result =  //Get the Result
        "return":
         "status": 200, "message": "Accept" ,
        "entries":
        [
             "messageid": 40859622, "message": "Text1", "status": 14, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431868680, "cost": 146 ,
             "messageid": 40859621, "message": "Text2", "status": 10, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431868680, "cost": 146 ,
             "messageid": 40859589, "message": "Text3", "status": 14, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431868620, "cost": 146 ,
             "messageid": 40858860, "message": "Text4", "status": 10, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431867960, "cost": 146 ,
             "messageid": 40858483, "message": "Text5", "status": 10, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431867600, "cost": 146 
        ]
    ;

    var aaData = result.entries; //aaData will have all the entries.


希望这会有所帮助。

【讨论】:

以上是关于如何为 jQuery DataTables 获取 JSON 的某些部分的主要内容,如果未能解决你的问题,请参考以下文章

如何为dataTables启用多列过滤器?

如何为 dataTables 中的元素运行 popover?

如何用 jQuery 中的 dataTables 替换标准引导表?

如何为 Kendo UI jQuery 的电子表格重新加载数据?

如何将过滤器和订单数据设置到 DataTables 单元格

jquery如何为元素设置style?