我正在通過 Ajax JSON 使用資料表填充一個表,這是我的 JS 代碼:
$(document).ready(function () {
$.ajax({
url: "../WebService.asmx/LoadUsers",
type: 'POST',
datatype: 'json',
//content: 'json', lo mismo que arriba
contentType: 'application/json; charset=utf-8',
success: function (data) {
var datos = JSON.parse(data.d);
// METODO JQUERY DATATABLES Documentación
$('#tblUsers').DataTable({
data: datos,
columns: [
{ 'data': 'id' },
{ 'data': 'username' },
{ 'data': 'password' },
{ 'data': 'dregistered' },
{
'data': null,
'defaultContent': "<img src='../assets/img/delete.png' id='btnDel' style='width: 22px; cursor:pointer;' />"
}
//
],
responsive: true
});
/*DataTables instantiation.*/
/*$("#tblUsers").DataTable();*/
},
error: function () {
alert('Fail!');
}
});
});
html表:
<table id="tblUsers" class="table table-bordered nowrap" style="width:100%">
<thead>
<tr>
<th>Id</th>
<th>Usuario</th>
<th>Contrase?a</th>
<th>Fecha</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>Usuario</th>
<th>Contrase?a</th>
<th>Fecha</th>
</tr>
</tfoot>
</table>
加載html頁面后我的表 沒有顯示資料,一開始似乎沒有呈現,但是在應用列過濾器或更改條目后,資料顯示: 按條目編號過濾
按列順序過濾
我錯過了什么嗎???
uj5u.com熱心網友回復:
您的問題可能是您正在嘗試設定 5 列資料(即使第 5 列有data: null),但您的 HTML 中只有 4 列<table>。
如果您查看您的控制臺,可能應該有一些類似Cannot read property 'style' of undefined或類似的錯誤。因此,表內資料的加載被錯誤中斷。
將您的 HTML 更改為此(添加額外的<th></th>),它應該沒問題:
<table id="tblUsers" class="table table-bordered nowrap" style="width:100%">
<thead>
<tr>
<th>Id</th>
<th>Usuario</th>
<th>Contrase?a</th>
<th>Fecha</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>Usuario</th>
<th>Contrase?a</th>
<th>Fecha</th>
<th></th>
</tr>
</tfoot>
</table>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464954.html
標籤:javascript html json 阿贾克斯 数据表
