2016-03-07 57 views
3

我有一个网页的名称的数据表:数据表删除重复的信息/隐藏行中

|ID|Page   |Domain | 
---------------------------- 
|1 |test.com/page1|test.com| 
|2 |test.com/page2|test.com| 
|3 |xyz.net/page1 |xyz.net | 
|4 |xyz.net/page2 |xyz.net | 

我想要做的就是隐藏(不是组)与重复的域行,只显示第一个结果针对每一个域:

|ID|Page   |Domain | 
---------------------------- 
|1 |test.com/page1|test.com| 
|3 |xyz.net/page1 |xyz.net | 

我知道如何hide rows based on their value,但我不知道我该怎么做上面的事情。

我是否必须使用帮助器映射/数组编写我自己的一段代码,其中我将存储已显示的域?或者有什么更聪明的方法如何在数据表中做到这一点?

回答

3

是的。我相信最简单的方法是首先找到我们想要排除的重复项,然后使用自定义筛选器仅显示那些未排除的行。

遍历所有的行,标记列其中域已经存在的“排除”:

var domain, domains = [] 
table.rows().every(function(rowIdx, tableLoop, rowLoop) { 
    domain = this.data().domain; 
    if (~domains.indexOf(domain)) { 
    this.nodes().to$().attr('excluded', 'true') 
    } else { 
    domains.push(domain) 
    } 
}) 

然后设置一个简单的自定义过滤器,只让未标记为行“排除”可见:

$.fn.dataTable.ext.search.push(function(settings, data, dataIndex) { 
    return table.row(dataIndex).nodes().to$().attr('excluded') != 'true' 
}) 

我们可以随时返回由

$.fn.dataTable.ext.search.pop() 
table.draw() 

演示,以显示所有行 - >http://jsfiddle.net/w75pdyf9/

+0

非常感谢大卫伟大的建议和例子 – Michal