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

狂风中的少年 提交于 2019-12-04 22:59:31

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.

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

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

You don't need to 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]);

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

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

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.

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'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!