I have a script I am trying to get to work. I am working in a dynamic table. I want to change the color of a cell based on the data. Below is my script. I get no errors and no results. Please help.
Thank you !!
var category = record.tables[“Funds”][0].fields[“category”];
var color = “”;
if (category === “Stable Value”) {
color = “#6f2a8a”; // purple
} else if (category === “Bond”) {
color = “#232c68”; // blue
} else if (category === “Equity”) {
color = “#39732f”; // green
} else if (category === “Balanced”) {
color = “#fda33d”; // yellow
} else if (category === “Company Stock”) {
color = “#980539”; // orange
} else {
color = “#ffffff”; // default gray
}
results.html(‘’);
var category = this.record.Funds.category;
var color = “”;
if (category === “Stable Value”) {
color = “#6f2a8a”; // purple
} else if (category === “Bond”) {
color = “#232c68”; // blue
} else if (category === “Equity”) {
color = “#39732f”; // green
} else if (category === “Balanced”) {
color = “#fda33d”; // yellow
} else if (category === “Company Stock”) {
color = “#980539”; // orange
} else {
color = “#ffffff”; // default gray
}
this.css('color',color)
Make sure to select the
@ahaddad , can you also let us know please what you have applied as selector to your script?
P.S. Rather than having that many else-if statements I would recommend to use a switch statement instead. Example:
switch (category) {
case "Stable Value":
color = "#6f2a8a";
break;
case "Bond":
color = "#232c68";
break;
case "Equity":
color = "#39732f";
break;
case "Balanced":
color = "#fda33d";
break;
case "Company Stock":
color = "#980539";
break;
default:
color = "#ffffff";
}
Instead of a switch statement you could also consider:
var category = this.record.Funds.category;
var colors = {
"Stable Value": "#6f2a8a", // purple
"Bond": "#232c68", // blue
"Equity": "#39732f", // green
"Balanced": "#fda33d", // yellow
"Company Stock": "#980539" // orange
};
this.css("color", colors[category] || "#ffffff"); // default gray
Thank you everyone. I am getting an error 'unknown field ‘Funds’. This is a dynamic table, does that make a difference?
Marten - to answer your question - I am using
My bad, forgot that when you use this.reocrd, the subdtable name is not used:
var category = this.record.category;
Hi - I have no errors but I do not have any colors either. I am using the selector #fundColor at the level
<td id="fundColor"
Can you please update this reply or share the information you wanted to share? Because it seems that your reply is missing some information here.
I’m sorry - in answer to your question as to what I am using for a selector. I am using <td id=“fundColor”.
Is there only 1 cell in 1 row you wish to setup a color to or could it be multiple row?
Yes - my dynamic table looks like this -
Row 1 cell 1 - Fund name
Row 1 cell 2 - this is where I want to change color based on the category
Row1 cell 3 - Category
Don’t worry. I understand why it wasn’t included. Please enclose any HTML you want to share between two backtick characters (one at each side). This way you can share any HTML. Example: <td id="fundColor">
I realize now that you’re talking about the background color, but the sample code that was given changes the font color. Use “background-color” instead of “color”.
1 Like