How to highlight repeated/duplicated data under same column?
How to highlight repeated/duplicated data under same column?

HI everyone,
Currently I'm trying to highlight the whole row which under repeated username condition but unfortunately I didn't find any related solution for this problem. My idea is to search for the same username and then detect the username appear twice then only highlight that row.
Below provided the code for highlight username only. Hope you guys can give some feedback. Thank you.
//datatable in javacript
//data[1] is the username row but i want to search in column
rowCallback: function(row, data) {
if ((data[1] === data[1]) && ((data[1] === data[1]) >= 2)) {
$('td:eq(1)', row).css('color', 'blue');
}
}
This question has accepted answers - jump to:
This discussion has been closed.
Answers
This example may help - it's highlighting duplicate rows,
Colin
Hi Colin,
glad to say that the code is working from my side.
Could you please explain the idea of these code?
You will want to read the following Datatable API docs:
column()
column().data()
toArray()
Also these Javascript docs:
indexOf()
lastIndexOf()
column(1).data()
returns an instance of the Datatable API which includes all the data from column 1. ThetoArray()
returns just the data in a Javascript array.Checks to see if the first index and last index of the specific row
data[i]
in column 1 are unequal, meaning more than one. If unequeal it will set the background-color to red.Does this answer your questions?
Kevin
Thanks Kevin.
By the way, i had one more question about this function. If I want to compare 2 or more column at the same time, is it possible to add condition to the if statement like below code?
var allData = this.api().column(1).data().toArray();
var snd_data = this.api().column(2).data().toArray();
if (allData.indexOf(data[1]) != allData.lastIndexOf(data[1])) {
$('td:eq(1)', row).css('background-color', 'Red');
}
else if ((allData.indexOf(data[1]) != allData.lastIndexOf(data[1])) && (snd_data.indexOf(data[2]) != snd_data.lastIndexOf(data[2])){
$('td:eq(1)', row).css('background-color', 'blue');
}