Using django-tables2 has been a great pleasure. I'm now trying to figure out how to alter css for rows, based on the row content. For example, given the table:
header 1 | header 2
---------|---------
value 1 | 1
value 2 | 1
value 3 | 2
value 4 | 2
value 5 | 2
value 6 | 3
value 7 | 3
I'd like to be able to style sequential rows with identical 'header 2' values with the same class. With itertools.groupby, it's easy enough to group the values that should be grouped, but i can't figure out how to move through the table object and update the class attrs for each row.
For columns, one can dynamically update the attrs. For example, in init, self.columns['ColumnName'].column.attrs = {'td': {'class': 'foo'}} works. However, for rows, this (and various permutations) does not work:
for i in self.rows:
i.attrs = {'tr': {'class': 'foo'}} # *** AttributeError, can't set attribute
i.cells.row.attrs = {...} # *** AttributeError
i.cells.row.attrs['class'] = 'foo' # Seems to work, but attrs not updated
# same as above
i.cells.row.attrs.update({'tr': {'class': 'foo'}})
i.table.row_attrs = {'class': 'foo'} # works, but applies to the whole table at once.
Clearly i have the wrong end of the stick here. The documentation suggests using row_attrs in class Meta and that can have a function to return the class value. However, in that function, one can only see one row at a time (passed as the 'record' object), whereas in this case one needs to parse the table as a whole, so init seems like a better place to address this from.
A solution to this sticky situation or any pointers would be much appreciated.
row_attrs, with a callable. But it will need to keep some state, so a simple lambda won't do. Why do you need to 'parse the whole table'? - Jieter