Use*_*384 9 javascript asp.net jquery datatables jquery-datatables
下面是文档就绪功能
Script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "GetUser.ashx",
"sServerMethod": "POST",
"sAjaxDataProp" : "",
"aoColumnDefs": [ {
"aTargets": [ 0 ],
"mData": "download_link",
"mRender": function ( data, type, full ) {
return '<a href="/UserDetail.aspx?ID='+data+'">Detail</a>';
}
} ],
"aoColumns": [
{ "mData": "LoginId" },
{ "mData": "Name" },
{ "mData": "CreatedDate" }
]
});
Run Code Online (Sandbox Code Playgroud)
以下是来自服务器的响应(GetUser.ashx)
[
{
"UserId": "1",
"LoginId": "white.smith",
"Activated": "Y",
"Name": "Test Account",
"LastName": "Liu",
"Email": "white.smith@logical.com",
"CreatedDate": "1/21/2014 12:03:00 PM",
"EntityState": "2",
"EntityKey": "System.Data.EntityKey"
},
More Data...
]
Run Code Online (Sandbox Code Playgroud)
下面是应该放置数据的html表
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th width="15%">User Detail</th>
<th width="15%">LoginID</th>
<th width="15%">Name</th>
<th width="15%">Created Date</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="dataTables_empty">Loading data from server</td>
</tr>
</tbody>
<tfoot>
<tr>
<th width="15%">User Detail</th>
<th width="15%">LoginID</th>
<th width="15%">Name</th>
<th width="15%">Created Date</th>
</tr>
</tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)
预期结果:

但我遇到了一个问题:
在加载页面时,浏览器中有一个未捕获的异常:
Cannot read property 'length' of undefined
Run Code Online (Sandbox Code Playgroud)
当我进一步检查时,它来自jquery.dataTables.js的第2037行
var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );
Run Code Online (Sandbox Code Playgroud)

我检查了json是否有效,但是"aData"为空,为什么会发生这种情况?
您"sAjaxDataProp" : ""的设置为空字符串,这会导致此错误.
dataTables希望在这里有一个字符串来告诉你服务器返回数据的哪个键可以找到.这默认为aaData,因此您的json应该在分页等可能返回或需要的所有其他键中包含此键.
正常的服务器json:
{
"iTotalRecords":"6",
"iTotalDisplayRecords":"6",
"aaData": [
[
"1",
"sameek",
"sam",
"sam",
"sameek@test.com",
"1",
""
],...
Run Code Online (Sandbox Code Playgroud)
由于所有值都在aaData中,因此根本不需要sAjaxDataProp.
修改后的服务器端json:
{
"iTotalRecords":"6",
"iTotalDisplayRecords":"6",
"myData": [
[
"1",
"sameek",
"sam",
"sam",
"sameek@test.com",
"1",
""
],
Run Code Online (Sandbox Code Playgroud)
现在值在myData中.所以你需要通过设置告诉dataTables在哪里找到实际数据:
"sAjaxDataProp" : "myData"
Run Code Online (Sandbox Code Playgroud)
这是一个Plunker
小智 6
由于有4列,请在"aoColumns"中添加以下内容:
"aoColumns": [
{ "mData": null }, // for User Detail
{ "mData": "LoginId" },
{ "mData": "Name" },
{ "mData": "CreatedDate" }
]
Run Code Online (Sandbox Code Playgroud)
对于未定义的长度,我尝试了以下代码并且它正在工作:
$('#example').dataTable({
"aLengthMenu": [[100, 200, 300], [100, 200, 300]],
"iDisplayLength": 100,
"iDisplayStart" : 0,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "GetUser.ashx",
"sServerMethod": "POST",
"sAjaxDataProp" : "",
"aoColumnDefs": [ {
"aTargets": [ 0 ],
"mData": "download_link",
"mRender": function ( data, type, full ) {
return '<a href="/UserDetail.aspx?ID='+data+'">Detail</a>';
}
} ],
"aoColumns": [
{ "mData": null },
{ "mData": "LoginId" },
{ "mData": "Name" },
{ "mData": "CreatedDate" }
]
});
Run Code Online (Sandbox Code Playgroud)
要了解更多信息的参考站点aLengthMenu是:
https://legacy.datatables.net/ref#aLengthMenu
| 归档时间: |
|
| 查看次数: |
51620 次 |
| 最近记录: |