我有以下 HTML 表格:
Date Desc
2018-10-01 Description 1
2018-10-01 Description 2
2018-10-01 Description 3
2018-10-01 Description 4
2018-10-02 Description 1
2018-10-02 Description 2
2018-10-02 Description 3
我想要的是这样的:
Date Desc
2018-10-01 Description 1
Description 2
Description 3
Description 4
2018-10-02 Description 1
Description 2
Description 3
我试过的是这样的:
var seen = {};
$('#TableId tbody tr').each(function () {
var txt = $("td:first-child", $(this)).text();
if (seen[txt]) $(this).text("");
else seen[txt] = true;
});
但是有了这个我得到了以下输出:
Date Desc
2018-10-01 Description 1
2018-10-02 Description 1
编辑
这是 Codepen
更新
根据 Adam 的回答,现在工作正常,但问题是在我的应用程序中更改了 Desc 列的排序顺序,这是我的真实数据的示例
我做错了什么?我该如何解决?
最佳答案
添加 fnDrawCallback 应该可以解决您的排序问题:
$('#TableId').DataTable({
"fnDrawCallback": updateTable
});
function updateTable(){
var x = '';
$('#TableId tbody tr').each(function () {
var txt = $("td:first-child", $(this)).text();
if(txt == x) {
$("td:first-child", $(this)).css('visibility', 'hidden');
}
else {
$("td:first-child", $(this)).css('visibility', 'visible');
x = txt;
}
});
}
关于javascript - 删除列中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52787737/