Javascript JQuery 数据表:无法读取未定义的属性“aDataSort”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28454203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Datatables : Cannot read property 'aDataSort' of undefined
提问by swateek
I created this fiddle to and it works well as per my requirements: Fiddle
我创建了这个小提琴,它按照我的要求运行良好:Fiddle
However, when I use the same in my application I get an error in the browser console saying Cannot read property 'aDataSort' of undefined
但是,当我在我的应用程序中使用它时,我在浏览器控制台中收到一条错误消息,提示无法读取未定义的属性“aDataSort”
In my application, the javascript reads something like as below: I have checked the controller output...it works well and is printed on the console too.
在我的应用程序中,javascript 读取的内容如下所示: 我检查了控制器输出...它运行良好并且也打印在控制台上。
$(document).ready(function() {
$.getJSON("three.htm", function(data) {
// console.log("loadDataTable >> "+JSON.stringify(data));
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
alert(err);
console.log( "Request Failed: " + err);
})
.success(function(data){
loadDataTable(data);
});
function loadDataTable(data){
$("#recentSubscribers").dataTable().fnDestroy();
var oTable = $('#recentSubscribers').dataTable({
"aaData" : JSON.parse(data.subscribers),
"processing": true,
"bPaginate": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"aoColumnDefs": [{
"sTitle": "Subscriber ID",
"aTargets": [0]
}, {
"sTitle": "Install Location",
"aTargets": [1]
}, {
"sTitle": "Subscriber Name",
"aTargets": [2]
}, {
"aTargets": [0],
"mRender": function (data, type, full) {
return '<a style="text-decoration:none;" href="#" class="abc">' + data + '</a>';
}
}],
"aoColumns": [{
"mData": "code"
}, {
"mData": "acctNum"
}, {
"mData": "name"
}]
});
}
})
回答by Gkrish
It's important that your THEAD not be empty in table.As dataTable requires you to specify the number of columns of the expected data . As per your data it should be
重要的是您的 THEAD 在 table.As 中不能为空。因为 dataTable 要求您指定预期数据的列数。根据您的数据,它应该是
<table id="datatable">
<thead>
<tr>
<th>Subscriber ID</th>
<th>Install Location</th>
<th>Subscriber Name</th>
<th>some data</th>
</tr>
</thead>
</table>
回答by hogarth45
Also had this issue, This array was out of range:
也有这个问题,这个数组超出范围:
order: [1, 'asc'],
回答by Oliver Zendel
For me, the bug was in DataTables itself; The code for sorting in DataTables 1.10.9 will not check for bounds; thus if you use something like
对我来说,错误在于 DataTables 本身;DataTables 1.10.9 中的排序代码不会检查边界;因此,如果你使用类似的东西
order: [[1, 'asc']]
with an empty table, there is no row idx 1 -> this exception ensures. This happened as the data for the table was being fetched asynchronously. Initially, on page loading the dataTable gets initialized without data. It should be updated later as soon as the result data is fetched.
对于空表,没有行 idx 1 -> 此异常确保。这是因为正在异步获取表的数据。最初,在页面加载时,dataTable 在没有数据的情况下被初始化。一旦获取结果数据,它应该在稍后更新。
My solution:
我的解决方案:
// add within function _fnStringToCss( s ) in datatables.js
// directly after this line
// srcCol = nestedSort[i][0];
if(srcCol >= aoColumns.length) {
continue;
}
// this line follows:
// aDataSort = aoColumns[ srcCol ].aDataSort;
回答by Abdul Manan
I faced the same problem, the following changes solved my problem.
我遇到了同样的问题,以下更改解决了我的问题。
$(document).ready(function() {
$('.datatable').dataTable( {
bSort: false,
aoColumns: [ { sWidth: "45%" }, { sWidth: "45%" }, { sWidth: "10%", bSearchable: false, bSortable: false } ],
"scrollY": "200px",
"scrollCollapse": true,
"info": true,
"paging": true
} );
} );
the aoColumnsarray describes the width of each column and its sortableproperties.
该aoColumns数组描述了每列的宽度及其sortable属性。
Another thing to mention this error will also appear when you order by a column number that does not exist.
当您按不存在的列号进行排序时,还会出现要提及此错误的另一件事。
回答by JumpingElephant
In my case I had
就我而言,我有
$(`#my_table`).empty();
Where it should have been
应该在的地方
$(`#my_table tbody`).empty();
Note: in my case I had to empty the table since i had data that I wanted gone before inserting new data.
注意:在我的情况下,我不得不清空表,因为在插入新数据之前我有想要删除的数据。
Just thought of sharing where it "might" help someone in the future!
只是想分享它在未来“可能”帮助某人的地方!
回答by Ryan Shillington
I had this problem and it was because another script was deleting all of the tables and recreating them, but my table wasn't being recreated. I spent ages on this issue before I noticed that my table wasn't even visible on the page. Can you see your table before you initialize DataTables?
我遇到了这个问题,这是因为另一个脚本正在删除所有表并重新创建它们,但我的表没有被重新创建。在我注意到我的表格在页面上甚至不可见之前,我在这个问题上花了很长时间。在初始化 DataTables 之前你能看到你的表吗?
Essentially, the other script was doing:
本质上,另一个脚本正在做:
let tables = $("table");
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
if ($.fn.DataTable.isDataTable(table)) {
$(table).DataTable().destroy(remove);
$(table).empty();
}
}
And it should have been doing:
它应该一直在做:
let tables = $("table.some-class-only");
... the rest ...
回答by MohitGhodasara
You need to switch single quotes [']to double quotes ["]because of parse
由于解析,您需要将单引号切换[']为双引号["]
if you are using data-orderattribute on the table then use it like this data-order='[[1, "asc"]]'
如果您在表上使用data-order属性,则像这样使用它data-order='[[1, "asc"]]'

