javascript - jquery 数据表包含空白表头

标签 javascript jquery json datatable

我正在尝试使用 jquery datatables插件通过从 PHP 脚本返回的 JSON 对象填充 html 表。我对 json 对象的命名和数据表插件中的选项有一些困惑。像这样传回 json 对象:

echo json_encode(array('data'=>$res));

产量:

Uncaught TypeError: Cannot read property 'length' of undefined 

将数据更改为aaData时成功填充表格,但没有标题。

几个论坛帖子提到 json 对象必须作为 aaData 返回,但其网站上的数据表示例文档给出了 example以数据命名的 json 对象。

这是我当前的客户端代码,我相对确定我的服务器端是正确的。

$('document').ready(function(){
    $('#result').dataTable({
        "ajax": {
            'url' : 'ppmAlarms.php',

        },

        "columns": [
            { "data": "location" },
            {"data" : "descAndType" },
            {"data" : "timestamp"}
        ]
    });

    });

JSON结构如下:

{
  "aaData": [
    {
      "location": "loc1",
      "descAndType": "desc1",
      "timestamp": "2014-08-20 11:34:10"
    }
]
}

最佳答案

刚刚意识到我只需要在 thead 中指定我自己的列标题,它们不会被填充:

<thead>
      <tr>
         <th>Name</th>
         <th>Position</th>
         <th>Office</th>
         <th>Extn.</th>
         <th>Start date</th>
         <th>Salary</th>
     </tr>
</thead>

关于javascript - jquery 数据表包含空白表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433012/

相关文章:

javascript - Onclick 更改脚本标签内的功能

json - Jquery $.Ajax 卡住浏览器

javascript - 为什么 settimeout() 匿名函数不调用 JavaScript 中的函数?

javascript - 如何在 null 上设置颜色输入的值

json - Struts ajax 表单验证

java - 使用 JSONArray 和 JSONObject 进行 Foreach

javascript - 自动扩展文本区域后跳转滚动条

php - 如何在 15 天后自动删除数据库中的一行数据?

jquery - Squarespace 代码块对齐问题

javascript - 遇到 css 边距和 div 图像约束问题,希望得到一些建议或提示