javascript - DataTables 不显示 JSON 数据

标签 javascript jquery html json datatable

这是我的HTML

    <table id="dt">
        <thead>
            <tr>
                <th>make</th>
                <th>model</th>
                <th>serial</th>
                <th>status</th>
                <th>User</th>
                <th>dept</th>
                <th>location</th>
        </thead>
        <tbody>
            
        </tbody>
        <tfoot></tfoot>
    </table>

这是我的JS

    <script type="text/javascript" language="javascript" >
         
        $(document).ready(function() {
            
            $.post("json.php",function(data){
                $('#dt').DataTable( {
                    "aaData": data,
                    "aoColumns": [
                            {"mDataProp": "make"},
                            {"mDataProp": "model"},
                            {"mDataProp": "serial"},
                            {"mDataProp": "status"},
                            {"mDataProp": "user"},
                            {"mDataProp": "dept"},
                            {"mDataProp": "location"}
                        ]
                });
            });
    
        } );
    </script>

这里是 json.php

    
    $data = Array ();
    $data[] = array("make" => "Hp", "model" => "jhbh", "serial" => "kjkhn", "status" => "active", "user" => "John Doe", "dept" => "Manufacturing Services", "location" => "Bindura");
    $data[] = array("make" => "Dell", "model" => "Vostro", "serial" => "kjkhn", "status" => "active", "user" => "Percy Holdin", "dept" => "Manufacturing Services", "location" => "Kwekwe");
    
    echo json_encode($data,JSON_PRETTY_PRINT);

我已经对这个问题进行了编辑,因为现在我想获取动态数据。

错误:

DataTables warning: table id=dt - Requested unknown parameter 'make' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

但是我没有从那个帮助链接中找到任何有用的东西

最佳答案

JSON 格式应如下例所示:https://datatables.net/examples/data_sources/ajax.html

{
    "data": [
        [
            "Hp",
            "jhbh",
            "kjkhn",
            "active",
            "John Doe",
            "Manufacturing Services",
            "Bindura"
        ]
    ]
}

关于如何在 php 中像这样格式化 json 的示例(好吧,一种方式):

$data = (object) [
    'data' => [[
        'test',
        'test',
        'test'
    ]]
];
echo json_encode($data);

还有实例:http://sandbox.onlinephpfunctions.com/code/632c288c6c743da25e49958c204a8d4e0a936b54

关于javascript - DataTables 不显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46682669/

相关文章:

Jquery点击函数+css

python - Django:访问模板中的列表属性

python - 使用或不使用 ModelForms 创建新帐户并使用 HTML/CSS 进行设计

javascript - Ajax jquery 成功范围

javascript - 在网页中创建 Div 需要多少内存?

javascript - Facebook Graph API 只返回用户名

javascript - 使用 phantomjs 调用函数给出的结果与从控制台调用的结果不同

javascript - Rethinkdb 如果分支计算结果为 false,则使用现有值

javascript - 如何创建更好的方法来维护基于我的代码访问的最后一页

javascript - 如何为我的菜单创建流畅的动画?