本文介绍了数据表无法读取未定义的属性“长度"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是文档准备功能

Script type="text/javascript" charset="utf-8">$(document).ready(function () {$('#example').dataTable({bProcessing":真,bServerSide":真,"sAjaxSource": "GetUser.ashx","sServerMethod": "POST","sAjaxDataProp" : "",aoColumnDefs":[ {"aTargets": [ 0 ],"mData": "下载链接",mRender":函数(数据,类型,完整){return '<a href="/UserDetail.aspx?ID='+data+'">Detail</a>';}}],"aoColumns": [{ "mData": "LoginId" },{mData":名称"},{mData":创建日期"}]});

下面是来自服务器的响应(GetUser.ashx)

[{"用户 ID": "1","LoginId": "white.smith","已激活": "Y","Name": "测试账户","LastName": "刘","电子邮件": "[email protected]","CreatedDate": "1/21/2014 12:03:00 PM","EntityState": "2","EntityKey": "System.Data.EntityKey"},更多数据...]

下面是数据应该放的html表格

<头><tr><th width="15%">用户详细信息</th><th width="15%">LoginID</th><th width="15%">名称</th><th width="15%">创建日期</th></tr></thead><tr><td colspan="5" class="dataTables_empty">从服务器加载数据</td></tr></tbody><脚><tr><th width="15%">用户详细信息</th><th width="15%">LoginID</th><th width="15%">名称</th><th width="15%">创建日期</th></tr></tfoot>

预期结果:

但是我遇到了一个问题:

在加载页面时,浏览器出现未捕获的异常:

无法读取未定义的属性长度"

当我进一步检查时,它来自 jquery.dataTables.js 的第 2037 行

var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );

我检查了 json 是否有效,但aData"为空,为什么会发生这种情况?

解决方案

由于有4列,在aoColumns"中添加以下内容:

"aoColumns": [{ "mData": null },//用于用户详细信息{ "mData": "LoginId" },{mData":名称"},{mData":创建日期"}]

对于未定义的长度,我尝试了以下代码并且可以正常工作:

$('#example').dataTable({"aLengthMenu": [[100, 200, 300], [100, 200, 300]],iDisplayLength":100,iDisplayStart":0,bProcessing":真,bServerSide":真,"sAjaxSource": "GetUser.ashx","sServerMethod": "POST","sAjaxDataProp" : "",aoColumnDefs":[ {"aTargets": [ 0 ],"mData": "下载链接",mRender":函数(数据,类型,完整){return '<a href="/UserDetail.aspx?ID='+data+'">Detail</a>';}}],"aoColumns": [{ "mData": null },{ "mData": "LoginId" },{mData":名称"},{mData":创建日期"}]});

了解更多关于 aLengthMenu 的参考站点是:

https://legacy.datatables.net/ref#aLengthMenu

Below is the document ready function

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" }
            ]
        });

Below is the respond from server (GetUser.ashx)

[
    {
        "UserId": "1",
        "LoginId": "white.smith",
        "Activated": "Y",
        "Name": "Test Account",
        "LastName": "Liu",
        "Email": "[email protected]",
        "CreatedDate": "1/21/2014 12:03:00 PM",
        "EntityState": "2",
        "EntityKey": "System.Data.EntityKey"
    },
More Data...
]

Below is the html table where the data should be put

<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>

Expected result:

But I came across a problem:

While the page is loading, there was an uncaught exception from the browser:

Cannot read property 'length' of undefined

When I further check, it came from line 2037 of jquery.dataTables.js

var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );

I checked that the json was valid, but the "aData" was null, why this happen?

解决方案

As there are 4 columns, add the following in "aoColumns":

"aoColumns": [
  { "mData": null },  // for User Detail
  { "mData": "LoginId" },
  { "mData": "Name" },
  { "mData": "CreatedDate" }
]

For undefined length, I have tried the following code and it's working:

$('#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" }
  ]
});

The reference site to know more about aLengthMenu is:

https://legacy.datatables.net/ref#aLengthMenu

这篇关于数据表无法读取未定义的属性“长度"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:10