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).

    评论

报告相同问题?