JavaScript-DataTable错误-无法读取未定义的属性’length’

我正在尝试使用对服务的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"
       }

一旦我做完了……桌子就可以完美地建起来了!

上一篇:CodeGo.net>如何使用数据表绑定修改数据网格视图列


下一篇:CodeGo.net>将数据表转换为私有列表>?