如何将存储在txt文件中的JSON数据显示给Datatable?

问题描述 投票:0回答:2

我有一个存储在.txt文件中的JSON文件,我想将它显示在我的数据表中。继this tutorial之后,我在JS上设置了“ajax”路径。

$(document).ready(function() {
    $('#example').DataTable( {
        "ajax": "data/objects.txt"
    } );
} );

但是,如果我在JS中设置“列”,则显示数据表的模式不会显示,并且所有jQuery事件都会失败。

"columns": [
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "extn" },
            { "data": "start_date" },
            { "data": "salary" }
        ]

我错过了什么?

jquery json ajax codeigniter datatable
2个回答
0
投票

我建议改变

"ajax": "data/objects.txt"

"ajax": "/data/objects.txt"

注意领先的/。您的URL是相对URL,因此系统将使用完整的URL,然后添加data/objects.txt。例如

https://www.yourdomain.com/some/path/data/objects.txt

使用领先的/使其成为根相对URL,因此它将始终路径:

https://www.yourdomain.com/data/objects.txt

0
投票

确保你有正确的结构json

按照这个json文件:https://datatables.net/examples/ajax/data/objects.txt

{
    "data": [{
        "id": "1",
        "name": "Tiger Nixon",
        "position": "System Architect",
        "salary": "$320,800",
        "start_date": "2011/04/25",
        "office": "Edinburgh",
        "extn": "5421"
    },
    {
        "id": "2",
        "name": "Garrett Winters",
        "position": "Accountant",
        "salary": "$170,750",
        "start_date": "2011/07/25",
        "office": "Tokyo",
        "extn": "8422"
    }]
}

因为无法访问txt文件,我给你这个例子展示怎么样,希望对你有所帮助

$(document).ready(function() {
    //https://datatables.net/examples/ajax/data/objects.txt
    var data =[{
            "id": "1",
            "name": "Tiger Nixon",
            "position": "System Architect",
            "salary": "$320,800",
            "start_date": "2011/04/25",
            "office": "Edinburgh",
            "extn": "5421"
        },
        {
            "id": "2",
            "name": "Garrett Winters",
            "position": "Accountant",
            "salary": "$170,750",
            "start_date": "2011/07/25",
            "office": "Tokyo",
            "extn": "8422"
        }];
    $('#example').DataTable({
        //"ajax": "https://datatables.net/examples/ajax/data/objects.txt",
        "data": data,
        "columns": [{
            "data": "name"
        },
        {
            "data": "position"
        },
        {
            "data": "office"
        },
        {
            "data": "extn"
        },
        {
            "data": "start_date"
        },
        {
            "data": "salary"
        }]
    });
});
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
热门问题
推荐问题
最新问题