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

时间:2019-04-24 02:43:05

标签: jquery json ajax codeigniter datatable

我有一个存储在.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" }
        ]

有什么我想念的吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>