I would like to vary the background color of individual cells of a table (not entire rows or columns), based on values in other cells. Is this possible?
Here are some sample data:
Type Vessel Value
A 10 2.1
B 12 2.5
C 23 1.9
A 13 2.3
B 15 2.0
C 20 1.5
And the logic might be something like:
If type = A
Value<1.5 Then background color = White
Value between 1.5 and 2 Then background color = Yellow
Value >2 Then background color = REd
And so on for each Type.
The built-in coloring feature only works for coloring entire rows, however by making clever use of the fact that table cells can be HTML formatted, you can achieve the effect that you describe. The logic is done in the SQL query itself.
OK, I can see how that would work. However, will this prevent me from doing any calculations using the contents of the cell? For example, I need to allow users to select one or more non-contiguous rows, and display the weighted averages of certain cells (the weighting is determined by the contents of a given column). Will I need to in code search out the number portion of the cell contents (the right hand most 3 characters, for example), convert them to numbers, then calculate averages?
Ah, good point. If you use HTML to color the numbers, the cell contents becomes Strings instead of Numbers, and it becomes awkward to do programmatic calculations of these numbers. You have 2 options here:
One is to do as you suggested - have your numeric calculation code try to parse out the digits in the HTML strings and use those. I don’t recommend this - very fragile.
Option two is better. The Table component has the ability to hide certain columns from view. It is important to know that these hidden columns can still be accessed programmatically, however. The idea here is to select the HTML formatted data, and then the raw data (use a different column name using the SQL “AS” clause), and hide the raw data.
Lets say your data looked like this:
(Columns)Type, Vessel, Value, RawValue
(Ex. Data) “A”, 10, “…2.1”, 2.1
Now, tell the table to hide the “RawValue” column.
The following code will calculate the average of the selected RawValues:
[code]#This line will vary depending on your window
table = event.source.parent.getComponent(“Table”)
selectedRows = table.getSelectedRows()
if len(selectedRows)>0:
total = 0
for rowNum in selectedRows:
value = table.data.getValueAt(rowNum, 3) # 3 is the column index of “RawValue”
total += value
average = total / len(selectedRows)
print "Average is: ", average
[/code]