Wednesday, September 28, 2011

smartgrid: currency format and text-align


controllers/default.py
def admin():
    products = SQLFORM.grid(db.Product,deletable=False, paginate=10)
    return dict(products = products)

models/db.py
db.define_table('Product',
    Field('Part_Number'),
    Field('List_Price', 'decimal(13,2)'))

views/default.py

{{extend 'layout.html'}}
<h2>Product Table</h2>
{{=products}}

By default, all text is aligned to left. You can align it right for decimal fields by adding the following in db.py:

db.Product.List_Price.represent = lambda value, row: XML(DIV(value,_style='text-align: right;')) 


or currency format:

db.Product.List_Price.represent = lambda value, row: '$ %.2f' % (0.0 if value == None else value)


or both:


db.Product.List_Price.represent = lambda value, row: DIV('$ %.2f' % (0.0
if value == None else value), _style='text-align: right;')

Ref: https://groups.google.com/group/web2py/browse_thread/thread/97a3e861957e758b/21bdc438c387abd3#21bdc438c387abd3


No comments:

Post a Comment