javascript - 如何使用 json 填充 jquery 数据表?

标签 javascript jquery json datatable

我在我的应用程序中使用 jquery 数据表,插件链接 https://datatables.net/

我想用 JSON 填充我的数据表,但失败了。这是我的代码。

HTML:

<table id="example" class="display" width="100%" cellspacing="0">
                            <thead>
                            <tr>
                                <th>id</th>
                                <th>Name</th>
                                <th>Code</th>
                                <th>Description</th>
                                <th>isActive</th>
                            </tr>
                            </thead>
                            <tfoot>
                            <tr>
                                <th>id</th>
                                <th>Name</th>
                                <th>Code</th>
                                <th>Description</th>
                                <th>isActive</th>
                            </tr>
                            </tfoot>
                            <tbody>
                            </tbody>
                        </table>

JS:

$(document).ready(function() {
    console.log("hi ready");
    $('#example').DataTable({

        retrieve: true,
        ajax: {

            url: '/ProductLicensingApplication/feature/list',
            dataSrc: ''
        },

        "columns": [
            { "data": "id" },
            { "data": "name" },
            { "data": "code" },
            { "data": "description" },
            { "data": "isActive" }
        ]
    });
} );

我的json json data

但我无法将数据填充到表格中,因为表格显示表格中没有可用的数据..您可以在图片中看到

enter image description here 请告诉我我的代码有什么问题...

最佳答案

documentation 中所写。 ajax.dataSrc 选项用于告诉 DataTables 数据数组在 JSON 结构中的位置。空字符串是一种特殊情况,它告诉 DataTables 需要一个数组。

在您的情况下,JSON 是一个对象,您应该设置 dataSrc : 'features'

关于javascript - 如何使用 json 填充 jquery 数据表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536002/

相关文章:

javascript - JQuery 背景颜色动画不起作用

java - 从 Map 实例化 JSONObject 时出错 : java. lang.NoSuchMethodError

用于日志记录的 Java 对象序列化

jquery - Jquery 表单验证问题

javascript - 在 WordPress 中使用 Jquery 和 css 区分两个相同的 HTML ID 元素

javascript - 当用户刷新整个页面时防止 bootstrap 3 模式关闭

javascript - 将元素附加到 arr.each 循环中的父元素

javascript - 悬停后暂停视频

javascript - Google 图表未在浏览器/JSON 中显示

javascript - 使用图像作为背景并应用于同级的 div parent