簡體   English   中英

DataTable錯誤 - 無法讀取未定義的屬性“長度”

[英]DataTable Error - Cannot read property 'length' of undefined

我正在嘗試使用ajax調用服務來構建我的DataTable(1.10.5) - http://www.datatables.net/examples/ajax/

這是我的Javascript:

$('#tableexample').DataTable({

    "dom": 'C<"clear">lfrtip',
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../../api/EventTypes/GetAll",
    "aoColumnDefs": [
      {
          "aTargets": [0],
          "mData": "Id"
      },
       {
           "aTargets": [1], 
           "mData": "Name"
       },
       {
           "aTargets": [2],
           "mData": "Name"
       },
       {
           "aTargets": [3],
           "mData": "Name"
       },
       {
           "aTargets": [4],
           "mData": "Name"
       }
    ]
});

這是我的HTML:

<table id="tableexample" class="table table-striped dataTable table-hover">
                        <thead>
                            <tr>
                                <th>Select</th>
                                <th>Event</th>
                                <th>Primary Category</th>
                                <th>Secondary Category</th>
                                <th>Workflow</th>
                            </tr>
                        </thead>
                    </table>

這是我的錯誤:

Uncaught TypeError: Cannot read property 'length' of undefined

如果我看我的jquery.dataTables.js - 它說我的數據是未定義的...

var data = _fnAjaxDataSrc( settings, json );

誰能幫助我正確設置我的ajax調用以動態構建我的表?

謝謝!

終於找到了!

我需要進行ajax調用並將數據傳遞給“aaData”:

$.ajax({
    url: '/Portal/api/EventTypes/GetEventWorkflowDefinitions',
    type: 'GET',
    dataType: 'json',
    success: function (data) {
        assignToEventsColumns(data);
    }
});

function assignToEventsColumns(data) {
var table = $('#tableexample').dataTable({
    "dom": 'C<"clear">lfrtip',
    "bAutoWidth": false,
    "aaData": data,
    "aaSorting": [],
    "aoColumnDefs": [
       {
           "aTargets": [0],
           "bSearchable": false,
           "bSortable": false,
           "bSort": false,
           "mData": "EventTypeId",
           "mRender": function (event) {
               return '<input class="childCheck" type="checkbox" id="childCheckBoxes" value="' + event + '">';
           }
       },
       {
           "aTargets": [1], 
           "mData": "EventType"
       }

一旦我這樣做了......桌子就完美了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM