Money-formatted numbers in SQLFORM grid

By Indra Yana on May 19, 2014 in Happy / 1 comment

In web2py SQLFORM grid, numbers are shown unformatted and left-aligned. It will be better if the numbers or money values are currency formatted, to make it easier to read. And better yet if those numbers right-aligned in the grid / table. We can use represent in models to do the job, like:

db.define_table(
    'bill',
    ...
    Field('comm', 'decimal(15,2)', label=T('Comm.')),
    Field('total', 'decimal(15,2)', label=T('Total')),
    ...
    )

db.bill.comm.represent = lambda value, row: DIV(moneyfmt(value), _style='text-align: right;')
db.bill.total.represent = lambda value, row: DIV(moneyfmt(value), _style='text-align: right;')<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>

I use function moneyfmt() posted here to format the numbers, shown in grid :

Discussion