Javascript 未捕获的类型错误:无法读取未定义的属性“aDataSort”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30348028/
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
Uncaught TypeError: Cannot read property 'aDataSort' of undefined
提问by Abdul Manan
i am working on pagination and i am using DataTablesplugin , on some tables it's work but on some tables it gives error:
我正在处理分页,我正在使用DataTables插件,在某些表上它可以工作,但在某些表上它给出错误:
Uncaught TypeError: Cannot read property 'aDataSort' of undefined
未捕获的类型错误:无法读取未定义的属性“aDataSort”
my page script looks like:
我的页面脚本如下所示:
$(document).ready(function() {
$('.datatable').dataTable( {
"scrollY": "200px",
"scrollCollapse": true,
"info": true,
"paging": true
} );
} );
//HTML code
//HTML代码
<table class="table table-striped table-bordered datatable">
<thead>
<tr>
<th><?php echo lang('date_label')?></th>
<th><?php echo lang('paid_label')?></th>
<th><?php echo lang('comments_label');?></th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $pay): ?>
<tr>
<td><?php echo dateformat($pay['time_stamp'], TRUE);?></td>
<td><?php echo format_price($pay['amount']);?></td>
<td><?php echo $pay['note'];?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
no idea how the problem comes ,i know this is very common error but i search and found nothing supporting my problem .
does anyone knows the solution ?
不知道问题是怎么来的,我知道这是很常见的错误,但我搜索并没有发现任何支持我的问题的错误。
有谁知道解决方案?
回答by Nikos M.
use something like the following in your code to disable sorting on DataTables(adapted from a project of mine which uses latest DataTables)
在您的代码中使用类似以下内容来禁用排序DataTables(改编自我的一个使用 latest 的项目DataTables)
$(document).ready(function() {
$('.datatable').dataTable( {
'bSort': false,
'aoColumns': [
{ sWidth: "45%", bSearchable: false, bSortable: false },
{ sWidth: "45%", bSearchable: false, bSortable: false },
{ 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, adjust as needed for your own table (number of) columns.
该aoColumns数组描述了每列的宽度及其sortable属性,根据您自己的表(数)列的需要进行调整。
回答by pgcan
I faced same issue and later found a typing mistake in "targets" property under columnDefs. See below example,
我遇到了同样的问题,后来在 columnDefs 下的“目标”属性中发现了一个打字错误。看下面的例子,
WRONG code below,
下面的代码错误,
{
"name": "firstName",
"target": [1],
"visible": false
}
Correction - missed 's' in targets :(
更正 - 在目标中错过了 's' :(
{
"name": "firstName",
"targets": [1],
"visible": false
}
Looks like this error occurs when something causing columns not getting initialized. I checked that event 'preInit.dt' is not fired in this case.
当某些导致列未初始化的事情时,看起来会发生此错误。我检查了在这种情况下没有触发事件“preInit.dt”。
Hope this helps someone.
希望这可以帮助某人。
回答by Greg Veres
I ran into this issue using KnockoutJS because the columns were not yet defined when the javascript was trying to apply the DataTable to it. My approach with knockoutJS was to move my data-bind code into a knockout template and use the afterRender event to apply the DataTable to the table.
我使用 KnockoutJS 遇到了这个问题,因为当 javascript 试图将 DataTable 应用于它时,列尚未定义。我使用knockoutJS 的方法是将我的数据绑定代码移动到一个knockout 模板中,并使用afterRender 事件将DataTable 应用到表中。
Here is a link to the knockoutJS docsfor the template afterRender event.
这是模板afterRender 事件的knockoutJS 文档的链接。
Here is what my data-bind looks like:
这是我的数据绑定的样子:
<div data-bind="template: { name: 'schedule-table', data: $data, afterRender: setupDataTable }"></div>
There was one more trick. In the setupDataTable function, the table still isn't setup completely, (I was trying to get the fixedHeaders and the widths weren't setup yet). So I called setTimeout with a 0 millisecond delay to get the code to run on the first idle cycle.
还有一招。在 setupDataTable 函数中,表格仍然没有完全设置,(我试图获得 fixedHeaders 并且尚未设置宽度)。所以我用 0 毫秒的延迟调用了 setTimeout 来让代码在第一个空闲周期运行。
Here is my setupDataTable:
这是我的 setupDataTable:
function setupDataTable() {
setTimeout(function() {
$("#schedule").DataTable({fixedHeader: true});
}, 0);
}
Hope this helps somebody else looking for a knockoutJS solution and running into the same problem I ran into.
希望这可以帮助其他人在寻找 KnockoutJS 解决方案并遇到我遇到的相同问题。
回答by maia
This error showed up for me, I believe because my "mData" and "sTitle" were undefined.
这个错误出现在我身上,我相信是因为我的“mData”和“sTitle”未定义。

