简体   繁体   English

如何用json填充jquery数据表?

[英]how to populate jquery datatables with json?

I am using jquery datatable in my application, plugin link https://datatables.net/ 我在我的应用程序中使用jquery datatable,插件链接https://datatables.net/

I want to populate my datatable with JSON, but I am failed.here is my code. 我想用JSON填充数据表,但失败了,这是我的代码。

HTML: 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: 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" }
        ]
    });
} );

my json 我的json json资料

but I am not able to populate data into the table as the table shows no data available in the table..you can see in the image 但是我无法将数据填充到表格中,因为表格在表格中没有显示可用数据。您可以在图像中看到

在此处输入图片说明 please tell me what is the problem in my code... 请告诉我代码中的问题是什么...

As written in documentation . 文档中所写。 The ajax.dataSrc option is used to tell DataTables where the data array is in the JSON structure. ajax.dataSrc选项用于告诉DataTables数据数组在JSON结构中的位置。 An empty string is a special case which tells DataTables to expect an array. 空字符串是一种特殊情况,它告诉DataTables需要一个数组。

In your case JSON is an object and you should set dataSrc : 'features' 在您的情况下,JSON是一个对象,应该设置dataSrc : 'features'

Ahmad, 艾哈迈德,

Either set dataSrc : 'features' or if possible rename the attribute name 'features' to 'data' in the response data. 在响应数据中设置dataSrc : 'features'或在可能的情况下将属性名称'features'重命名为'data'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM