DataTables remove / hide rows with duplicate information in column

Viewed 6002

I have a datatable with names of web pages:

|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 |

What I want to do is hide (not group) rows with duplicate domain and display only first result for every domain:

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

I know how to hide rows based on their value, but I wonder how can I do the above thing.

Do I have to write my own piece of code with a helper map/array, in which i'll store domain that I already displayed? Or is there any smarter way how to do this in datatables?

2 Answers

Using JavaScript dictionary

  var dictionary  = {};
                table = document.getElementById("YourTableID");
                tr = table.getElementsByTagName("tr");
                for (i = 0; i < tr.length; i++) {             
                    td = tr[i].getElementsByTagName("td")[0]; // `index for filter`        
                    if (dictionary[td.textContent]) {
                        tr[i].style.display = "none";
                    } else {
                        seen[td.textContent] = true;
                    }
                }
Related