Examples of customizing SQLFORM.grid add/edit/view screens?

1,530 views
Skip to first unread message

A36_Marty

unread,
Jan 29, 2014, 12:13:27 PM1/29/14
to web...@googlegroups.com

I've created a web2py grid of records based on the selection on the page.  i.e. a table COMPANY has many EMPLOYEES -- someone selects a given COMPANY and a SQLFORM.grid of the related EMPLOYEES is shown.

Question:  How can I customize the web2py Add/Edit forms that appear when the respective buttons are pushed on the Grid.

More specifically, if one wishes to add an EMPLOYEE, I need to hide the related COMPANY field on the add form, yet still fill in the value with whatever COMPANY is currently selected.

I see formargs, editargs, createargs, etc., but am having trouble finding examples (that I understand) of how to modify these forms and not recreate the wheel by making totally custom add/edit grid forms.

Your responses are most appreciated.  I am a novice web2py user with limited webapp development experience.

Thanks in advance.

Jim S

unread,
Jan 29, 2014, 2:58:46 PM1/29/14
to web...@googlegroups.com
So, do you want to modify the layout of the add/edit forms, or just default in the COMPANY on an add?

this line:    I see formargs, editargs, createargs, etc., but am having trouble finding examples (that I understand) of how to modify these forms and not recreate the wheel by making totally custom add/edit grid forms.

....makes me think you don't want to alter the layout.  Looking for clarification.

-Jim

A36_Marty

unread,
Jan 29, 2014, 4:24:32 PM1/29/14
to web...@googlegroups.com
Thanks for your reply.  Might be the clearest to respond with code:

<Models>

db.define_table('company',
    Field('name','string' ))

db.define_table('employees',
    Field('company_id', db.company),
    Field('name', 'string'))

<Controller>

def manage_employees():
    employees_for_company1 = db().select(db.employees.company_id == 1)   
    grid = SQLFORM.grid(employees_for_company1)


My question is when I press "ADD" on the grid, I get a new screen in which "company_id" is visible and blank.   How would I customize the "add" form created by the grid to 1) not show the company_id field yet also fill it in with "1" before the insert to make sure the newly added employee is related to the currently viewed company - company_id "1" in this case?

Many thanks for your help.

Jim Steil

unread,
Jan 29, 2014, 4:28:49 PM1/29/14
to web...@googlegroups.com
Here is what I would do.

def manage_employees():
    company_id = 1
    employees_for_company1 = db().select(db.employees.company_id == company_id)   
    if request(0) == 'new':
        db.employees.company_id.default = company_id
        db.employees.company_id.writable = False
        db.employees.company_id.readable = False

    grid = SQLFORM.grid(employees_for_company1)



--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/n9uinYLDFZ4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jim Steil

unread,
Jan 29, 2014, 4:30:06 PM1/29/14
to web...@googlegroups.com
You can query request(0) for either 'new', 'edit', 'view' and I think 'delete' to see what operation you're acting on.  

-Jim

Halili Celik

unread,
Apr 11, 2015, 12:00:41 AM4/11/15
to web...@googlegroups.com
instead of request(0), write request.args(0) 

29 Ocak 2014 Çarşamba 19:13:27 UTC+2 tarihinde A36_Marty yazdı:

Felix Penetrante

unread,
Jun 1, 2015, 2:04:21 PM6/1/15
to web...@googlegroups.com
Thanks.  This code is so helpful.  It worked perfectly.

“This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required please request a hard-copy version.” Bicol University, Legazpi City, Philippines.
“This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required please request a hard-copy version.” Bicol University, Legazpi City, Philippines.
Reply all
Reply to author
Forward
0 new messages