[英]Jquery Datatables and Jeditable - Data is not displaying
I am using Jquery DataTables and jEditable. 我正在使用Jquery DataTables和jEditable。 I have a correct JSON response as follows: 我有一个正确的JSON响应,如下所示:
[{"country_id":"18","country":"Aruba","country_enabled":"1"},{"country_id":"19","country":"Afghanistan","country_enabled":null},{"country_id":"22","country":"Angola","country_enabled":"1"},{"country_id":"23","country":"Anguilla","country_enabled":null},{"country_id":"24","country":"\u00c5land Islands","country_enabled":null},{"country_id":"25","country":"Albania","country_enabled":null},{"country_id":"26","country":"Andorra","country_enabled":null},{"country_id":"27","country":"United Arab Emirates","country_enabled":null},{"country_id":"29","country":"Argentina","country_enabled":null},{"country_id":"30","country":"Armenia","country_enabled":null},{"country_id":"31","country":"American Samoa","country_enabled":null},{"country_id":"32","country":"Antarctica","country_enabled":null},{"country_id":"33","country":"French Southern Territories","country_enabled":null},{"country_id":"34","country":"Antigua and Barbuda","country_enabled":null},{"country_id":"35","country":"Australia","country_enabled":null},{"country_id":"36","country":"Austria","country_enabled":null},{"country_id":"37","country":"Azerbaijan","country_enabled":null},{"country_id":"38","country":"Burundi","country_enabled":null},{"country_id":"39","country":"Belgium","country_enabled":null},{"country_id":"40","country":"Benin","country_enabled":null},{"country_id":"41","country":"Bonaire, Sint Eustatius and Saba","country_enabled":null},{"country_id":"42","country":"Burkina Faso","country_enabled":null},{"country_id":"43","country":"Bangladesh","country_enabled":null},{"country_id":"44","country":"Bulgaria","country_enabled":null},{"country_id":"45","country":"zoo","country_enabled":null},{"country_id":"46","country":"Xylaphone","country_enabled":null}]
The above is taken from Chrome's developer tools and from the XHR window and, therefore, I know the response looks correct and the data is being received. 以上内容来自Chrome的开发人员工具和XHR窗口,因此,我知道响应看起来正确并且正在接收数据。
Here is my HTML to display the data: 这是显示数据的HTML:
<div class="content">
<div id="pad-wrapper" class="form-page">
<div class="row">
<div class="col-md-12">
<h2>List of Countries</h2>
</div>
<div class="bs-example">
</br>
<form>
<div align = "left">
<button type="button" class="btn btn-success" onclick="window.location='<?php echo site_url("admin/country_add");?>'">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Country
</button>
<button type="button" class="btn btn-danger" onclick="window.location='<?php echo site_url("admin/country_delete");?>'">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span> Delete Countries
</button>
</div>
<!-- start table listing -->
<table id="myDataTable">
<thead>
<tr>
<th>country_id</th>
<th>country</th>
<th>country_enabled</th>
</thead>
<tbody>
</tbody>
</table>
<button id="btnAddNewRow">Add</button>
<button id="btnDeleteRow">Delete</button>
</div>
</div>
</div>
The datatable appears in the view but just says, "loading..." and no data is ever displayed. 数据表出现在视图中,但仅显示“正在加载...”,并且从未显示任何数据。
I have renamed the column headers the same as my database but it still does not display the data. 我已将列标题重命名为与数据库相同的名称,但是它仍然不显示数据。
There is an error in the console as follows: 控制台中有一个错误,如下所示:
In the client-side processing mode data provided via Ajax should have the following structure, see ajax option for more details. 在客户端处理模式下,通过Ajax提供的数据应具有以下结构,有关更多详细信息,请参见ajax选项。
{
"data": [
// row 1 data source,
// row 2 data source,
// etc
]
}
The solution is to correct your JSON data to look like: 解决方案是将JSON数据更正为:
{
"data": [
{"country_id":"18","country":"Aruba","country_enabled":"1"}
]
}
Alternative solution is to set ajax.dataSrc to empty string to indicate that you're returning plain array, see the sample code below: 替代解决方案是将ajax.dataSrc设置为空字符串以指示您将返回纯数组,请参见下面的示例代码:
$('#example').dataTable({
"ajax": {
"url": "data.json",
"dataSrc": ""
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.