I learned this method from one of Massimo's lecture video's: At the top of your model file: # function to allow models to set status style def show_status(status,row=None): return SPAN(status.f_name.replace(' ','_'),_class=status.f_name) ifstatus else status
After your table definition (could be in your controller but I set the represent after my table definition in my model file so it is consistent across all locations in my app): db.table_name.status_field_name.represent = show_status Note that in my models db.table_name.status_field_name is a reference field to a status table (so you select from a drop down list to select a status) and the name of the status in the status table is f_name. In your CSS (I have one CSS static file for my application for application specific styles that I include in layout.html): /* Status styles */ .Not.Started {background-color: yellow; padding:5px;} .In.Progress {background-color: green; color:white; padding:5px;} .Completed {background-color: red; color:white; padding:5px;} .Deferred {background-color: blue; color:white; padding:5px;} I think this is nicer than embedding an if statement with hardcoded styles in your code. On Friday, May 9, 2014 4:22:45 PM UTC-4, LoveWeb2py wrote: > > Hello, > > I have a spreadsheet of items and I want to track their inventory based on > color (similar to an excel spreadsheet). > > I have two columns for example: Item, Item_Status > The item could be a desk, chair, etc... and for status I have 1 2 or 3. > > If the status is 1 I would like to make the cell color of the desk green, > 2 yellow, 3 red. > > so far I have > > def represent_colored(value): > if value == 1: > return SPAN(value,_style="background-color:green',) > elif value == 2: > return SPAN(value,_style="background-color:yellow',) > elif value == 3: > return SPAN(value,_style="background-color:red',) > else: > return SPAN(value,_style="background-color:orange',) > > def inventory(): > db.inventory.items.represent = lambda value, row: > represent_colored(value) > grid=SQLFORM.grid(db.inventory, paginate=20) > return dict(grid=grid) > > This works so far, but it only changes the background color of the text in > the status column and I want to change the column of the actual item. I > also would like to change the entire cell <td> instead of just the > background behind the text. > Also, I plan on branching the inventory out to multiple columns and I have > two questions: > > 1) How could I change the entire cell color? > 2) Is making multiple "status" columns the most efficient and pythonic way > to accomplish this. > > You guys are the best! > > > Wil > > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.