在动态修改的表上使用 jQuery tableSorter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/247305/
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
Using jQuery tableSorter on dynamically modified table
提问by Alex
I am using the jQuery tableSorter plugin on a page.
我在页面上使用 jQuery tableSorter 插件。
Unfortunatley, the table that is being sorted is dynamically modified, and when I sort after adding an element, the element disappears, restoring the table to the state that it was in when the tableSorter was created.
不幸的是,正在排序的表格是动态修改的,当我添加元素后排序时,元素消失,将表格恢复到创建 tableSorter 时的状态。
Is there any way that i can force tableSorter to rescan the page so that these new elements are sorted properly?
有什么方法可以强制 tableSorter 重新扫描页面,以便正确排序这些新元素?
回答by Josh
I believe you can trigger an update using something like:
我相信您可以使用以下内容触发更新:
$(table).trigger("update")
回答by Alex
Seems you are correct.
看来你是对的。
$(table).trigger("update"); $(table).trigger("appendCache");
does the trick.
诀窍。
As a note, the tablesorter API changed at some point, so these things got changed, as well as the event binding. My biggest hangup was trying to figure out why some things worked and others did not, and it was due to having a wrong version of the plugin, despite there being no obvious distinction.
需要注意的是,tablesorter API 在某些时候发生了变化,因此这些内容以及事件绑定都发生了变化。我最大的问题是试图弄清楚为什么有些事情有效而有些事情没有,这是由于插件版本错误,尽管没有明显的区别。
回答by Devaroop
The $(table).trigger("update");
throws error
在$(table).trigger("update");
抛出错误
Uncaught TypeError: Cannot read property 'rows' of undefined
So, there is a jquery function .ajaxStop()
where tablesorter()
is called. Do not call tablesorter in .ready()
所以,有一个jQuery函数.ajaxStop()
那里tablesorter()
被调用。不要调用tablesorter.ready()
jQuery(document).ajaxStop(function(){
jQuery("#table_name").tablesorter();
})
which did the job
哪个完成了工作
回答by Ishwar Rimal
For those Newbies like me who are facing issue with sorting dynamic generated table, here is the solution. The answer previously given are correct, but where do you place this command?
对于像我这样面临排序动态生成表问题的新手,这里是解决方案。前面给出的答案是正确的,但是你把这个命令放在哪里呢?
$('#tableId').tablesorter().trigger('update');
You need to place it as soon as you've appended the data to the table. Ex in my case
将数据附加到表后,您需要立即放置它。以我为例
var tableData = "<thead><tr><th>Name</th><th>Age</th></thead><tbody><tr><td>Izaki</td><td>24</td><tr><tr><td>Serizawa</td><td>25</td></tr>";
$('#tableId').html('tableData');
$('#tableId').tablesorter().trigger('update');