Add link to view a referenced table in SQLFORM.grid

1,238 views
Skip to first unread message

Thomas Sitter

unread,
Aug 24, 2015, 11:19:58 PM8/24/15
to web2py-users
I have two tables that I'd like to link in SQLFORM.grid.

db.define_table('customer',
 
Field('firstname'),
 
Field('lastname'),
 
Field('account'))

db
.define_table('survey_data',
 
Field('customer_id', 'reference customer'),
 
Field('Q1'),
 
Field('Q2'),
 
Field('Q3'))


In my SQLFORM I'd like to only show the firstname, lastname, and a link to view the survey_data for customers who have survey data.

def customer_view:
    query
=((db.customer.account > 100) & (db.customer.id==db.survey_data.customer_id))
    fields
=[db.customer.firstname, db.customer.lastname]
   
grid=SQLFORM.grid(deletable=False, editable=False, paginate=100, query=query, fields=fields)
    return dict(grid=grid)

Is there any way to add a link to view the survey data to this table? I want it to work the same way that clicking 'view' on the SQLFORM.grid automatically brings up a formatted customer information page.

Thanks!

Anthony

unread,
Aug 25, 2015, 2:26:14 AM8/25/15
to web2py-users
Have you considered SQLFORM.smartgrid?

Thomas Sitter

unread,
Aug 25, 2015, 7:03:41 AM8/25/15
to web...@googlegroups.com
I have, but I run into one issue -- the link brings me to a list of records, but this is a 1-to-1 relationship so it will always be unnecessary to display the list.

On Tuesday, 25 August 2015 02:26:14 UTC-4, Anthony wrote:
Have you considered SQLFORM.smartgrid?

Jim S

unread,
Aug 25, 2015, 9:00:48 AM8/25/15
to web2py-users
Here is what I do to put custom buttons on my grid view in SQLFORM.grid

In my Controller:

if not request.args(0) in ['view']:
 details
= False
 
links = [lambda row: getProductionDisplayAnchor(row)]


grid
= SQLFORM.grid(query, fields=fields,
 
orderby=orderby, left=left,
 create
=create, details=details,
 editable
=editable, deletable=deletable,
 csv
=False, search_widget=workorderSearch,
 paginate
=15, maxtextlength=45,
 links=links, formstyle=my_formstyle, ui=grid_ui)


And then the getProductionDisplayAnchor function

def getProductionDisplayAnchor(row):
   
"""
    build the anchor tags for the action buttons on the Production list
    page
    """

    anchor
= ''
   
try:
        workorderId
= row['workorder']['workorderId']
   
except:
        workorderId
= 0


   
if workorderId > 0:
        anchor
= A(SPAN(_class='glyphicon glyphicon-pencil') + (
       
' Record Usage' if db.workorder(workorderId).completed == None else 'View Usage'),
                   _href
=URL('production', args=('view', 'workorder', workorderId),
                             user_signature
=True),
                   _class
='btn btn-default') or ''


   
return anchor


From the web2py book:

links is used to display new columns which can be links to other pages. The links argument must be a list of dict(header='name',body=lambda row: A(...)) where header is the header of the new column and body is a function that takes a row and returns a value. In the example, the value is a A(...) helper.

You can find everything out about the SQLFORM.grid here:  http://web2py.com/books/default/chapter/29/07/forms-and-validators#SQLFORM-grid

-Jim

Thomas Sitter

unread,
Aug 25, 2015, 3:54:11 PM8/25/15
to web2py-users
This worked perfectly, thanks!
Reply all
Reply to author
Forward
0 new messages