2016-03-07 25 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