csdn产品小助手 2015-05-05 08:24 采纳率: 0%
浏览 23

Datatables.net深层对象

I'm following the deep objects example here: https://datatables.net/examples/ajax/deep.html

and I can get everything running fine with the example data. However when trying to integrate my own data I'm having an issue. The given Ajax data format is:

{
  "data": [
    {
      "name": "Tiger Nixon"
    }
 ]
}

with the columns defined as:

"columns": [
   { "data": "name" }
]

My data has the same layout but I have a different root name

{
  "root": [
    {
      "name": "Tiger Nixon"
    }
 ]
}

with the columns defined as:

"columns": [
   { "root": "name" }
]

Is the "data" keyword fixed? Or is it just default and I need to reassign it somewhere?

The error I receive is: "Uncaught TypeError: Cannot read property 'length' of undefined" which I take to mean it can't find the object it's trying to get the property of.

Thanks for any help!!

  • 写回答

1条回答 默认 最新

  • derek5. 2015-05-05 11:36
    关注

    If your JSON data looks like:

    {
      "root": [
        {
          "name": "Tiger Nixon"
        }
      ]
    }
    

    your DataTables initialization code should be:

    $('#example').DataTables({
       'ajax' : {
          'url': '/server/script.php',
          'dataSrc': 'root'
       },
       'columns': [
          'data': 'name'
       ]
    });
    

    From the manual:

    As a string, ajax.dataSrc defines the property from the data source object (i.e. that returned by the Ajax request) to read. It defaults to data property of the data source object (or aaData if data is not present for backwards compatibility).

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名