Modifying table's data binding styles

I feel that I have sorted out this problem before but can't remember how.

  • I have a session custom property with a query binding returning a dataset.
  • I want to change the color of a cell's background based on the value of another cell on the same row.

Can someone give me a pointer on how to do a script transform on the dataset binding?

In a nutshell, how do I convert the dataset into this format?

{
  "city": {
    "value": "Folsom",
    "editable": true,
    "style": {
      "backgroundColor": "#F7901D",
      "classes": "some-class"
    },
    "align": "center",
    "justify": "left"
  },
  "country": "United States",
  "population": 77271
}

Thanks.

I used this post in the past to change the background color with a transform.

This may be too magical/meta-programmy/functional for personal tastes; your mileage may vary.

One possible approach would be to break down the transformation operation into a value -> style decision, per 'cell' in the dataset:

def formatCity(value):
	return {
		"value": value,
		"editable": True,
		"style": {}
	}

def formatCountry(value):
	ret = {
		"value": value,
	}
	if (value == "United States"):
		ret["style"] = {
			"backgroundColor": "#FF0000"
		}
	return ret

metadata = {
	"city": formatCity,
	"country": formatCountry
}

return [
	{
		columnName: metadata.get(columnName, lambda v: v)(value.getValueAt(rowIndex, colIndex))
		for colIndex, columnName in enumerate(value.columnNames)
	}
	for rowIndex in xrange(value.rowCount)
]

Disclaimer: I haven't tested this code, so there's a decent chance it doesn't even run. But it might be suitable as a starting point for ideas.

1 Like

That's funny. I contributed to that post. I knew I had done this before.

I'm getting an "Object has no attribute 'rowCount'" error. I can't find the correct syntax on a web search.

The intention is for value to be a dataset, as if this were a transform script.