Jump to content

Changing ShadeColor based on a variable in data file


mattg

Recommended Posts

I am new to tables and I'm trying to work my way through it. The problem is that I need to change the ShadeColor of a cell based on a variable in my data file. I'm afraid I'm not that familiar with javascript to be able to set this up myself. I think the logic would be something like this.

 

/////////////////////

 

if (field("myVariable") == "A")

{

return (myTable.Rows[0].Cells[2].ShadeColor = "Green");

}

 

if (field("myVariable") == "B")

{

return (myTable.Rows[0].Cells[2].ShadeColor = "Blue");

}

 

else return "";

 

 

// then have my table setup below...

 

Thanks in advance.

Link to comment
Share on other sites

Dan,

 

Now I'm having an issue where I set a cell's color after I've created the table based on a variable. It will work for the first cell but it will not for the remaining two. See below.

 

myTable.Rows[0].Cells[0].Font="R Frutiger Roman";

myTable.Rows[0].Cells[0].TextColor="Black";

myTable.Rows[0].Cells[0].SetBorders("Medium", "Black", "Top");

myTable.Rows[0].Cells[0].SetBorders("Thin", "Gray", "Bottom");

myTable.Rows[0].Cells[0].Content = Field("numberPayments") + " payments @ $0 followed by";

myTable.Rows[0].Cells[0].VAlign = "Middle";

myTable.Rows[0].Cells[0].ShadePct = 10;

 

myTable.Rows[1].Cells[0].Font="R Frutiger Roman";

myTable.Rows[1].Cells[0].TextColor="Black";

myTable.Rows[1].Cells[0].SetBorders("Thin", "Gray", "Bottom");

myTable.Rows[1].Cells[0].Content = "Payment";

myTable.Rows[1].Cells[0].VAlign = "Middle";

myTable.Rows[1].Cells[0].ShadePct = 10;

 

myTable.Rows[2].Cells[0].Font="R Frutiger Roman";

myTable.Rows[2].Cells[0].TextColor="Black";

myTable.Rows[2].Cells[0].SetBorders("Thin", "Black", "Bottom");

myTable.Rows[2].Cells[0].Content = "Rate";

myTable.Rows[2].Cells[0].VAlign = "Middle";

myTable.Rows[2].Cells[0].ShadePct = 10;

 

//////////

 

if (Field("vendor") == "VendorA")

myTable.Rows[0].Cells[0].ShadeColor = "Forest";

myTable.Rows[1].Cells[0].ShadeColor = "Forest";

myTable.Rows[2].Cells[0].ShadeColor = "Forest";

 

if (Field("vendor") == "VendorB")

myTable.Rows[0].Cells[0].ShadeColor = "Warm Gray";

myTable.Rows[1].Cells[0].ShadeColor = "Warm Gray";

myTable.Rows[2].Cells[0].ShadeColor = "Warm Gray";

 

return myTable.MakeTags();

 

 

The example above will make the first row's cell color Forest, but the 2 remaining rows are set at Warm Gray. Where am I messing up?

 

Thanks in advance.

Link to comment
Share on other sites

You just need a few curly braces {}.

 

In JavaScript, if you want an "if" clause to affect more than one statement, you need to use curly brackets to denote a statement block, like so:

if (Field("vendor") == "VendorA")
[color="SeaGreen"]{[/color]
   myTable.Rows[0].Cells[0].ShadeColor = "Forest";
   myTable.Rows[1].Cells[0].ShadeColor = "Forest";
   myTable.Rows[2].Cells[0].ShadeColor = "Forest";
[color="SeaGreen"]}[/color]
//else
if (Field("vendor") == "VendorB")
[color="SeaGreen"]{[/color]
   myTable.Rows[0].Cells[0].ShadeColor = "Warm Gray";
   myTable.Rows[1].Cells[0].ShadeColor = "Warm Gray";
   myTable.Rows[2].Cells[0].ShadeColor = "Warm Gray";
[color="SeaGreen"]}[/color]

Although, you could do this as a switch statement instead:

switch (Field("vendor"))
{
   case "VendorA":
       myTable.Rows[0].Cells[0].ShadeColor = "Forest";
       myTable.Rows[1].Cells[0].ShadeColor = "Forest";
       myTable.Rows[2].Cells[0].ShadeColor = "Forest";
       break;

   case "VendorB":
       myTable.Rows[0].Cells[0].ShadeColor = "Warm Gray";
       myTable.Rows[1].Cells[0].ShadeColor = "Warm Gray";
       myTable.Rows[2].Cells[0].ShadeColor = "Warm Gray";
       break;
}

And this code can be simplified a bit, like so:

var color;
switch (Field("vendor"))
{
   case "VendorA":
       color = "Forest";
       break;

   case "VendorB":
       color = "Warm Gray";
       break;
}
myTable.Rows[0].Cells[0].ShadeColor = color;
myTable.Rows[1].Cells[0].ShadeColor = color;
myTable.Rows[2].Cells[0].ShadeColor = color;

Where the last three lines can be simplified to just:

for (var r = 0; r <= 2; r++)
   myTable.Rows[r].Cells[0].ShadeColor = color;

Or, more generally:

for (var r = 0; r < myTable.Rows.length; r++)
   myTable.Rows[r].Cells[0].ShadeColor = color;

If there are only those two choices, then the whole thing can be reduced even further:

for (var r = 0; r < myTable.Rows.length; r++)
   myTable.Rows[r].Cells[0].ShadeColor = (Field("vendor") == "VendorA") ?  "Forest" : "Warm Gray";

Or, if there are multiple choices, you can do something like this:

var VendorColors = {
   "VendorA"): "Forest",
   "VendorB" : "Warm Gray",
   "VendorC" : "Sky Blue",
   "VendorD" : "Dark Red",
   // etc.
}
for (var r = 0; r < myTable.Rows.length; r++)
   myTable.Rows[r].Cells[0].ShadeColor = VendorColors[Field("vendor")] || "Black"; // default to Black if not found

Or, if that's a bit much for you, you can simply create a Switch Wizard rule to map the vendors to colors, and simply assign the result of that rule, like so:

for (var r = 0; r < myTable.Rows.length; r++)
   myTable.Rows[r].Cells[0].ShadeColor = Rule("Vendor Color Rule");

Edited by Dan Korn
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...