gpt4 book ai didi

javascript - DataTable 错误 - 无法读取未定义的属性 'length'

转载 作者:行者123 更新时间:2023-11-29 10:13:08 26 4
gpt4 key购买 nike

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

一旦我这样做了…… table 就建好了!

关于javascript - DataTable 错误 - 无法读取未定义的属性 'length',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28906378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com