How to highlight repeated/duplicated data under same column?

How to highlight repeated/duplicated data under same column?

Rex_youRex_you Posts: 3Questions: 1Answers: 0
edited December 2020 in Free community support

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:

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    This example may help - it's highlighting duplicate rows,

    Colin

  • Rex_youRex_you Posts: 3Questions: 1Answers: 0

    Hi Colin,

    glad to say that the code is working from my side.
    Could you please explain the idea of these code?

    var allData = this.api().column(1).data().toArray();
          if (allData.indexOf(data[1]) != allData.lastIndexOf(data[1])) {
            $('td:eq(1)', row).css('background-color', 'Red');
          }
    
  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    Answer ✓

    You will want to read the following Datatable API docs:

    column()
    column().data()
    toArray()

    Also these Javascript docs:
    indexOf()
    lastIndexOf()

    var allData = this.api().column(1).data().toArray();

    column(1).data() returns an instance of the Datatable API which includes all the data from column 1. The toArray() returns just the data in a Javascript array.

    if (allData.indexOf(data[1]) != allData.lastIndexOf(data[1])) {

    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

  • Rex_youRex_you Posts: 3Questions: 1Answers: 0
    edited December 2020

    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');
    }

This discussion has been closed.