初始化时出现 jQuery TableSorter 插件错误:无法读取未定义的属性“0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8878945/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:11:29  来源:igfitidea点击:

jQuery TableSorter Plugin error on init : cannot read property '0' of undefined

jqueryjquery-pluginstablesorter

提问by Clawfire

i wanna sort my table with jQuery Plugin TableSorter . So i get this table :

我想用 jQuery 插件 TableSorter 对我的表格进行排序。所以我得到了这张桌子:

<table id="stats" class="zebra-striped">
 <thead>
  <tr>
   <th>Date</th>
   <th>Annonce</th>
   <th>Support</th>
   <th>Nb Assoc.</th>
   <th>Nb Transfo.</th>
   <th>Cout</th>
  </tr>
 </thead>
 <tbody>
 </tbody>
</table>

So as you can see my table is empty, just had header. So i init tablesorter with empty cell with :

所以你可以看到我的表是空的,只有标题。所以我用空单元格初始化 tablesorter :

$("table#stats").tablesorter({ sortList: [[0,0]]});

and immediatly i get this error :

我立即收到此错误:

jquery.tablesorter.min.js:4 Uncaught TypeError: Cannot read property '0' of undefined

jquery.tablesorter.min.js:4 Uncaught TypeError: Cannot read property '0' of undefined

FYI , there's my js loaded :

仅供参考,我的 js 已加载:

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if necessary -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.5.1.min.js">\x3C/script>')</script>
<script src="js/bootstrap-dropdown.js"></script>
<script src="js/bootstrap-scrollspy.js"></script>
<script src="js/jquery.tablesorter.min.js"></script>

Any idea why i get this and how i can make the plugin work again ?

知道为什么我得到这个以及如何使插件再次工作吗?

Thx

谢谢

采纳答案by JoJa

You need to have data in your table before you can call the sortList method on it. This is because you apply an indexing in this method that will not find any records if there is no data present and that will throw the "Cannot read property '0' of undefined" error.

您需要先在表中包含数据,然后才能对其调用 sortList 方法。这是因为您在此方法中应用了索引,如果没有数据存在,它将找不到任何记录,并且会引发“无法读取未定义的属性 '0'”错误。

回答by Michael Aguilar

It's not good use tablesorter when there is a empty table, so you can use this condition:

当有一个空表时,使用 tablesorter 是不好的,所以你可以使用这个条件:

if ($("table#stats tbody tr").length > 0)
   $(this).tablesorter({ sortList: [[0,0]]});

回答by Eneas Gesing

You don't needto have data in your table. Just initilize your table that way:

不需要在表中有数据。只需以这种方式初始化您的表:

$("table#stats").tablesorter();

Then, after you have inserted the data in the table, you must tell to plugin that the table was updated and sort it:

然后,在表中插入数据后,您必须告诉插件表已更新并对其进行排序:

$("table#stats").trigger("update");
var sorting = [[0,0]];
$("table#stats").trigger("sorton",[sorting]);

回答by Amaan

I couldn't get any of this to work so I set a timeout on the initialize for tablesorter...

我无法让任何这些工作,所以我在 tablesorter 的初始化上设置了超时......

setTimeout(function() {$('table').tablesorter();}, 10000);

回答by Greg

I got an error "cannot read property 'format' of undefined". In my case the error occurred due to different number of 'td's in 'tbody' than in 'thead'

我收到错误消息“无法读取未定义的属性‘格式’”。在我的情况下,由于“tbody”中的“td”数量与“thead”中的“td”数量不同而发生错误

回答by pinksy

I've noticed that this happens with the latest version (2.0.5b, I think) found at http://tablesorter.com/, but it didn't happen in earlier versions (I have a copy of 2.0.3, and it worked in that). However, there is a forked version at https://github.com/Mottie/tablesorter, which is much better maintained, and doesn't have this error.

我注意到在http://tablesorter.com/ 上找到的最新版本(我认为是 2.0.5b)会发生这种情况,但在早期版本中没有发生这种情况(我有 2.0.3 的副本,并且它在那工作)。但是,https://github.com/Mottie/tablesorter 上有一个分叉版本,维护得更好,并且没有此错误。