[英]DataTables Javascript data source 'No Data Available'
实时错误示例: http : //live.datatables.net/virobeci/1/edit?html,css,js,output
我有一个类似于以下内容的JavaScript对象:
{
"rows": [
{
"doc": {
"CustomerName": "abc",
"key": "value",
"keyabc": "value"
}
},
{
"doc": {
"CustomerName": "abc",
"key": "value",
"keyabc": "value"
}
}
]
}
我已经尝试了很长时间了,以使DataTables使用JavaScript数据源进行初始化,如下所述:
https://datatables.net/examples/data_sources/js_array.html
这就是我在做什么:
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' }
]
});
我没有错误,只是“表中没有可用数据”
表的定义如下:
<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>
实时示例: http : //live.datatables.net/virobeci/1/edit?html,css,js,output
编辑->请注意:
除了运行for循环并创建新数组以匹配示例数据源外,我无法更改数据源的格式。 我想知道是否可以使用这种类型的数据源初始化表
您的数据格式不正确:
var view = {
"rows": [
{
"CustomerName": "abc",
"key": "value",
"keyabc": "value"
},
{
"CustomerName": "abc",
"key": "value",
"keyabc": "value"
},
{
"CustomerName": "abc",
"key": "value",
"keyabc": "value"
}
]
};
而且,您必须将正确的数组分配为data.row(尽管在这种情况下,您可以简化结构,只删除一个级别)。
请注意,您必须将数据添加到“列”的“数据”属性中:
var table = $('#customersTable').DataTable( {
processing: true,
data: view.rows,
columns : [
{ data : 'CustomerName' },
{ data : 'CustomerName' },
{ data : 'CustomerName' },
{ data : 'CustomerName' },
{ data : 'CustomerName' },
{ data : 'CustomerName' }
]
});
您必须在“列”配置中提及列名称,然后您的数据就可以变平。
您可以尝试一下,示例代码在这里: http : //live.datatables.net/virobeci/2/edit?html,css,js,output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.