Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables Cannot read property 'length' of undefined

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:

enter image description here

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

enter image description here

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

like image 591
User2012384 Avatar asked Mar 18 '14 04:03

User2012384


People also ask

What does TypeError cannot read property'length'of undefined mean?

Show activity on this post. This errors TypeError: Cannot read property 'length' of undefined usually means that jQuery DataTables cannot find the data in the response to the Ajax request. By default jQuery DataTables expects the data to be in one of the formats shown below.

Why can't I access the data in jQuery DataTables?

Show activity on this post. Errors Unable to get property 'length' of undefined or null reference (IE) or Cannot read property 'length' of undefined (other browsers) with jQuery DataTables usually means that the plugin cannot access the data in response from Ajax request.

How to make DataTable expect an array instead of an object?

It's even simpler: just use dataSrc:'' option in the ajax defintion so dataTable knows to expect an array instead of an object: Show activity on this post. This errors TypeError: Cannot read property 'length' of undefined usually means that jQuery DataTables cannot find the data in the response to the Ajax request.


2 Answers

Your "sAjaxDataProp" : "" is set to an empty string, which causes this error.

dataTables expects to have a string here to tell under which key your server returned data can be found. This defaults to aaData, so your json should include this key amongst all others that might be returned or needed by pagination etc.

Normal serversided json:

{
"iTotalRecords":"6",
"iTotalDisplayRecords":"6",
"aaData": [
    [
        "1",
        "sameek",
        "sam",
        "sam",
        "[email protected]",
        "1",
        ""
    ],...

Since all values are in aaData you don't need sAjaxDataProp at all.

Modified serverside json:

{
"iTotalRecords":"6",
"iTotalDisplayRecords":"6",
"myData": [
    [
        "1",
        "sameek",
        "sam",
        "sam",
        "[email protected]",
        "1",
        ""
    ],

Now the values are in myData. so you need to tell dataTables where to find the actual data by setting:

"sAjaxDataProp" : "myData"

Here is a Plunker

like image 71
mainguy Avatar answered Oct 24 '22 11:10

mainguy


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

like image 32
Smruti Avatar answered Oct 24 '22 12:10

Smruti