使用json配置数据时出错

时间:2013-02-20 10:47:08

标签: jquery plugins datatable

我有一张表格如下

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
    <tr>
        <th>Month</th>
        <th>ID</th>
        <th>Comments</th>
        <th>Details</th>
    </tr>
</thead>
<tbody>
</tbody>
</table>

我的json是

[{
"Month": "Jan-2013",
"ID": "asdfa0",
"Comments": "",
"Details": "bla bla blba blablabalbal"
}]

我的脚本是

$(document).ready(function () {
var oTable = $('#example').dataTable({
    "bProcessing": true,
    "sAjaxSource": "Script/ahd.json",
    "aoColumns": [
    { "mData": "Month" },
    { "mData": "ID" },
    { "mData": "Comments" },
    { "mData": "Details" }
    ]
});
});

执行此操作后,我收到以下错误

未捕获的TypeError:无法读取未定义的jquery.dataTables.js的属性“length”:2649

经过一番谷歌搜索后,我得到了一个解决方案来包装json

{
  aaData: 
  [{
"Month": "Jan-2013",
"ID": "asdfa0",
"Comments": "",
"Details": "bla bla blba blablabalbal"
}]
}

这也行不通......这里有什么问题

1 个答案:

答案 0 :(得分:1)

阅读文档

你的js必须

$(document).ready(function() {
                $('#example').dataTable( {
                    "bProcessing": true,
                    "bServerSide": true,
                    "sAjaxSource": "Script/ahd.json",
                    "sServerMethod": "POST"
                } );
            } );

并且json格式必须像这样

{
  "sEcho": 3,
  "iTotalRecords": 6,
  "iTotalDisplayRecords": 3,
  "aaData": [
    [
      "A","B","C"
    ],
    [
       "A","B","C"
    ],
    [
        "A","B","C"
    ],
    [
        "A","B","C"
    ],
    [
       "A","B","C"
    ],
    [
       "A","B","C"
    ],
   ]

}