9

我创建了一个不时需要清除并重新填充的 jquery 表,我的清除方法是这样的:

//Clear table content before repopulating values
$('#table tr:gt(0)').remove();

现在我正在尝试使用 tablesorter 对特定列进行排序,但我的问题是当我启用 tablesorter 时:

//Initialize tablesorter
$("table").tablesorter();

清表方法已经不行了,它只是不断地在旧数据中添加新数据,造成大量重复信息。

请帮忙

4

4 回答 4

16

要更新 tablesorter,需要在更新其缓存之前触发“更新”

$('table').trigger('update');

您还可以使用内置函数清除表格

$.tablesorter.clearTableBody( table );

这是此演示中组合的代码

var $table = $('table');

// use built in clear function
$.tablesorter.clearTableBody( $table[0] );
$table
    .append('<tr><td>george</td><td>papard</td><td>68</td><td>19.99</td><td>55%</td><td>+3</td></tr>')
    .trigger('update');
于 2012-12-16T15:30:00.890 回答
1

我看不出它不应该工作的原因。它在没有 tablesorter 插件的情况下工作吗?

tablesorter()为什么正在调用table但尝试从中删除行#table?错字?

尝试记录 jQuery 对象$('#table'),看看那里的一切是否正确$('#table tr')$('#table tr:gt(0)')

于 2012-12-15T17:32:39.673 回答
0

我可以使它工作的唯一方法是重新生成整个表(删除它然后再次创建)。

$(".resultTablePlaceholder").html('').html('<table id="resultTable">...</table>');
$("#resultTable").tablesorter();
于 2013-10-15T14:36:24.633 回答
0

原因:

这是因为当我们在表中添加任何数据时,它会触发 buildCache() 方法并将表数据保存在缓存变量中,但是当您从表中删除数据时,它不会对缓存变量进行任何更改。

当您对数据进行排序时,它将清除表并将缓存的数据附加到表中,因此也存在已删除的数据。

解决方案:

您可以在 tablesorter.js 中修改 onselectstart() 函数或 mousedown 事件

//here tb_35 = id of table 
cache = buildCache($('#tb_35')[0]);
于 2017-08-01T09:33:22.307 回答