0

我有一张桌子如下

<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" }
    ]
});
});

这样做后,我收到以下错误

未捕获的类型错误:无法读取未定义 jquery.dataTables.js:2649 的属性“长度”

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

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

这也不起作用......这里有什么问题

4

1 回答 1

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"
    ],
   ]

}
于 2013-02-20T11:29:02.383 回答