Krisanda Triputro
Krisanda Triputro

Reputation: 3

How to change row color in datatables?

I am using datatables and currently stuck in changing a row to another color if value = INACTIVE, already tried many things but it has really weird error, my codes are :

"createdRow": function (row, data, dataIndex) {
        if (data[9] = "INACTIVE") {
            $(row).addClass("yellow");
        } else {
            $(row).addClass("white");
        }
    }

This code change all color row, but i want only change value INACTIVE

Thanks for the help!

Upvotes: 0

Views: 90

Answers (3)

BambiOurLord
BambiOurLord

Reputation: 372

In your if statement you are using a single '=' which is used for assignment. You should use double '=' to compare if the value is the same and triple '=' to compare if the value and the data types are the same.

You are also only checking index 9 of data. In your function you seem to also be passing in the index, you should instead change your code to something like this.

if (  data[ dataIndex ] === "INACTIVE" ) 

Upvotes: 0

Kizivat
Kizivat

Reputation: 173

In the condition, you are assigning the value "INACTIVE" to the data[9] instead of comparing the value. Subsequently, the condition only checks whether data[9] has some value, which is true, and class .yellow is always added.

So the condition should be like this if (data[9] == "INACTIVE") or rather if (data[9] === "INACTIVE") to perform check without type conversion.

Upvotes: 0

weigreen
weigreen

Reputation: 1242

You have a typo in your code.

In your if statement use == instead of =.

"createdRow": function (row, data, dataIndex) {
    if (data[9] == "INACTIVE") {
        $(row).addClass("yellow");
    } else {
        $(row).addClass("white");
    }
}

Upvotes: 2

Related Questions