【问题标题】:jquery datatables contains blank table headersjquery datatables 包含空白表头
【发布时间】:2014-08-21 18:01:01
【问题描述】:

我正在尝试使用 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"
    }
]
}

【问题讨论】:

    标签: javascript jquery json datatable


    【解决方案1】:

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

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

    【讨论】:

    • 我也摸不着头脑。
    • 但是如何从数据中动态添加标题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多