Table Cell Background Color Not Updating When Using Variable

Hi everyone,

I have a function that updates the background color of a table cell using a parameter called colorlung. Here’s the code I’m using:

return {'backgroundColor': colorlung, "color": "#FFFFFF", 'borderBottom': '3px solid red'}

When I pass a literal color (e.g., "#007FFF"), it works fine. However, when I try to use a variable (colorlung), the cell shows up completely white and the color is not applied.

Has anyone encountered this issue before? How can I ensure the cell uses the color stored in a variable?

Thanks in advance!

Show us how you've set the variable colorlung.

def getStyle(row,col,datechange,col2,colorlung,colorspes):		
		
		if ((cellprodid==self.custom.V_tag_exefoam) and (not cellprocessed==2)): #self.custom.V_tag_exefoam bind with tag EXE_FOAM
			if (cellstopfoaming==1 and not(datechange)):
				if col2 =="INVENTCOLORID":
					return {'backgroundColor':colorlung,"color": "#FFFFFF",'borderBottom': '3px solid red'}	
				return {'backgroundColor':'#0F6635',"color": "#FFFFFF",'borderBottom': '3px solid red'}
			elif (cellstopfoaming==1 and (datechange)):
				if col2 =="INVENTCOLORID":
					return {'backgroundColor':colorlung,"color": "#FFFFFF",'borderBottom': '3px solid red','borderTop': '8px double #4227F5'}	
				return {'backgroundColor':'#0F6635',"color": "#FFFFFF",'borderBottom': '3px solid red','borderTop': '8px double #4227F5'}
			elif (datechange):
				if col2 =="INVENTCOLORID":
					return {'backgroundColor':colorlung,"color": "#FFFFFF",'borderTop': '8px double #4227F5'}
				return {'backgroundColor':'#0F6635',"color": "#FFFFFF",'borderTop': '8px double #4227F5'}
			else:
				if col2 =="INVENTCOLORID":
					return {'backgroundColor':colorlung,"color": "#FFFFFF"}
				return {'backgroundColor':'#0F6635',"color": "#FFFFFF"}	

Thanks. We still can't see the value you're passing into the function.

You should be able to see the problem in Designer if you expand PROPS.data and examine the style property on the problem row or cell.

On a more general note: hard-coding style like this is not the way of the web or Perspective. Instead, create a style class for each condition and let your code apply the appropriate one. Consistency is then assured across your application and changes can be made in the styles without even opening the views.

Hello,

I´m using something like:

# Transform (Ignition Perspective) – colorea "TEMPERATURE C" por rangos

# Paleta básica
BLUE   = "#4BB4E6"
LBLUE  = "#B5E8F7"
GREEN  = "#50BE87"
YELLOW = "#FFD200"
ORANGE = "#FF9800"
RED    = "#CD3C14"

def color_temp(c):
    if c is None: return None
    if c < 18:  return BLUE
    if c < 20:  return LBLUE
    if c < 24:  return GREEN
    if c < 27:  return YELLOW
    if c < 30:  return ORANGE
    return RED

py  = system.dataset.toPyDataSet(value)
cols = list(py.getColumnNames())
out = []

for r in py:
    row = {}
    # Intenta convertir a float (si viene como texto, ignora si falla)
    try: t = float(str(r.get("TEMPERATURE C")).replace(".", "").replace(",", "."))  # tolera coma decimal
    except: t = None

    for c in cols:
        cell = {"value": r[c]}
        if c == "TEMPERATURE C":
            bg = color_temp(t)
            if bg: cell["style"] = {"backgroundColor": bg}
        row[c] = cell
    out.append(row)

return out


to colour cells on tables and works fine.

I´m using it as a script transform of binded data to props.data

Hi first thanks for coopeation,
The rest of the code is working fine. I have checked that the “colorlung“ variable have new color code every time but when try to use in style dictionery it cause the problem.

This work (without variable):

return {'backgroundColor':'#0F6635',"color": "#FFFFFF",'borderTop': '8px double #4227F5'}

This did not (with variable):

return {'backgroundColor':colorlung,"color": "#FFFFFF",'borderTop': '8px double #4227F5'}

Yes, but show us what the result is.

  • If you're in Designer then expand the PROPS.data and find the style prop for the affected cell or row.
  • If you're in a browser then press F12 for Developer Tools and use the Inspect Button to select the affected cell and examine the properties.

What do you mean by "style dictionary"?

Bad boy. Use style definitions. Don't hard code1.

1 There are times when you have to. The AM Charts components don't recognise CSS variables, for example. In that case I would create the color options as a session variable object and name each by what it represents rather than color (like the --success and --error theme variables, for example).

1 Like

Thanks a lot, Transistor!

It turns out there was a miscalculation in one of my if conditions. The data was formatted in Italian style as 3.800, and when I converted it to a float, it became 3.80 instead of 3800.00. I’ve fixed the conversion, and now everything is working fine.

As an aside, I'd find a rewrite like this to be a lot more readable/maintainable over time. Instead of building the whole dictionary in each possible branch, just make the corresponding updates in each branch, so if you want to add more conditions it's a lot easier and less error prone:

def getStyle(row, col, datechange, col2, colorlung, colorspes):
	if (cellprodid == self.custom.V_tag_exefoam) and (not cellprocessed == 2):
		# 1. Define a base style dictionary.
		style = {
			'backgroundColor': colorlung if col2 == "INVENTCOLORID" else '#0F6635',
			'color': '#FFFFFF'
		}

		# 2. Imperatively update the dictionary for conditional styles.
		if cellstopfoaming == 1:
			style['borderBottom'] = '3px solid red'
		
		if datechange:
			style['borderTop'] = '8px double #4227F5'
		
		# 3. Use a single return statement.
		return style

	# Else: Return empty dictionary to avoid dereferencing null
	return {}
2 Likes