how to use sum() in grid

44 views
Skip to first unread message

kesh

unread,
Jul 26, 2017, 9:27:33 AM7/26/17
to web2py-users
hello sir
i hope you are ok, well i have been having a problem with some code here and have searched all over the internet but i was not able to get a solution
MODEL
db.define_table('t_stck_issue',
    Field('f_name', type='reference t_items',
          label=T('Name')),
    Field('f_stck_client', type='reference t_clients',
          label=T('Client Assgn')),
    Field('f_stck_quantity', type='integer',
          label=T('Stock Quantity')),
    Field('f_unit_price', type='double',
          label=T('Unit Price')),
    Field('f_sub_total',type='double', compute=lambda r:r['f_unit_price']*r['f_stck_quantity'],
          label=T('Sub Total')),
    auth.signature,
    format='%(f_name)s',
    migrate=settings.migrate)

db.define_table('t_stck_issue_archive',db.t_stck_issue,Field('current_record','reference t_stck_issue',readable=False,writable=False))
CONTROLLER
def stck_issue_manage():
    #form = SQLFORM.smartgrid(db.t_stck_issue,onupdate=auth.archive)
    form = SQLFORM.grid(db.t_stck_issue)
    myrow=db().select(db.t_stck_issue.f_sub_total.sum())[0]
    answer=myrow._extra['SUM(t_stck_issue.f_sub_total)']
    return locals()
VIEW
{{extend 'layout.html'}}

<h2>Manage stck_issue</h2><p>{{=form}}</p>
<h2>All Issue Totals: {{=answer}}</h2>

well it calculates the sub total field for all properly, but i would like it to change when i filter by client to do the sub total for that particular client only

Massimo Di Pierro

unread,
Jul 29, 2017, 2:33:21 AM7/29/17
to web2py-users
There is no easy way but I just made a commit that fixes your problem. If you use trunk or wait for a next web2py version (within days) and you can do:

def stck_issue_manage():
    #form = SQLFORM.smartgrid(db.t_stck_issue,onupdate=auth.archive)
    grid = SQLFORM.grid(db.t_stck_issue)
    if grid.dbset:
         myrow=grid.dbset.select(db.t_stck_issue.f_sub_total.sum())[0]
         answer=myrow._extra['SUM(t_stck_issue.f_sub_total)']
    else: answer = None
    return locals()
VIEW
{{extend 'layout.html'}}

<h2>Manage stck_issue</h2><p>{{=grid}}</p>
<h2>All Issue Totals: {{=answer}}</h2>
Reply all
Reply to author
Forward
0 new messages