简体   繁体   English

DataTables Javascript数据源“无可用数据”

[英]DataTables Javascript data source 'No Data Available'

Live error example: http://live.datatables.net/virobeci/1/edit?html,css,js,output 实时错误示例: http : //live.datatables.net/virobeci/1/edit?html,css,js,output

I have a JavaScript object similar to this: 我有一个类似于以下内容的JavaScript对象:

{ 
    "rows": [
        {
            "doc": {
                "CustomerName": "abc",
                "key": "value",
                "keyabc": "value"
            }
        },
        {
            "doc": {
                "CustomerName": "abc",
                "key": "value",
                "keyabc": "value"
            }
        }
    ]
}

I've been trying for a long time to get DataTables to initialize using JavaScript datasource as mentioned here: 我已经尝试了很长时间了,以使DataTables使用JavaScript数据源进行初始化,如下所述:

https://datatables.net/examples/data_sources/js_array.html https://datatables.net/examples/data_sources/js_array.html

This is what I'm doing: 这就是我在做什么:

    var table = $('#customersTable').DataTable( {  
        data: view,
        columns: [
            { "rows" : 'doc.CustomerName' },
            { "rows" : 'doc.CustomerName' },
            { "rows" : 'doc.CustomerName' },
            { "rows" : 'doc.CustomerName' },
            { "rows" : 'doc.CustomerName' },
            { "rows" : 'doc.CustomerName' }
        ]  
    }); 

I get no errors, just 'No data available in table' 我没有错误,只是“表中没有可用数据”

Table is defined like this: 表的定义如下:

    <table id="customersTable" class="table table-striped table-bordered" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Number</th>
                <th>Login</th>
                <th>Party</th>
                <th>Sales-Id</th>
                <th>Sales-Party</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Number</th>
                <th>Login</th>
                <th>Party</th>
                <th>Sales-Id</th>
                <th>Sales-Party</th>
            </tr>
        </tfoot>

    </table>

Live example: http://live.datatables.net/virobeci/1/edit?html,css,js,output 实时示例: http : //live.datatables.net/virobeci/1/edit?html,css,js,output

Edit -> PLEASE NOTE: 编辑->请注意:

I can't change the format of the data source other than running a for loop and making a new array to match the sample data source. 除了运行for循环并创建新数组以匹配示例数据源外,我无法更改数据源的格式。 I wanna know if its possible to initialize the table with this type of data source 我想知道是否可以使用这种类型的数据源初始化表

You haven't form your data properly: 您的数据格式不正确:

var view = { 
"rows": [
    {
            "CustomerName": "abc",
            "key": "value",
            "keyabc": "value"
    },
    {
            "CustomerName": "abc",
            "key": "value",
            "keyabc": "value"
    },
  {
            "CustomerName": "abc",
            "key": "value",
            "keyabc": "value"
    }
]
};

And you have to assign the right array as data.row (although in your case you could simplify your structure a bit removing one level). 而且,您必须将正确的数组分配为data.row(尽管在这种情况下,您可以简化结构,只删除一个级别)。

Note that you will have to add the data to the 'data' property of 'columns': 请注意,您必须将数据添加到“列”的“数据”属性中:

var table = $('#customersTable').DataTable( { 
        processing: true,
        data: view.rows, 
        columns : [
            { data : 'CustomerName' },
            { data : 'CustomerName' },
            { data : 'CustomerName' },
            { data : 'CustomerName' },
            { data : 'CustomerName' },
            { data : 'CustomerName' }
        ]  
 }); 

you have to mention column name in your "columns" config and your data can be flattened. 您必须在“列”配置中提及列名称,然后您的数据就可以变平。

http://live.datatables.net/poganaso/2/edit http://live.datatables.net/poganaso/2/edit

您可以尝试一下,示例代码在这里: http : //live.datatables.net/virobeci/2/edit?html,css,js,output

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

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