styling smartgrid New/Edit/View forms

72 views
Skip to first unread message

icodk

unread,
May 25, 2017, 4:50:58 AM5/25/17
to web2py-users
Background
My model has a table with many fields (62). When using smartgrid to edit or add a record, all fields are shown in a long vertical list in the same order as defined in the model. So log so good.
This can be overwhelming to user that might need to see some of the fields very often and some others less frequently.
One way to solve this could be to show the fields in separate tabs that groups the fields according to some designation. Here is an example
Model:
db.define_table('person',
               
Field('first_name',tab='General', label=T('First name'), default='',length=50),
                Field('last_name',tab='General', label=T('Last name'), default='',length=50),
               Field('phone',tab='Advance',label=T('phone'), default=''),
                Field('visits',tab='Statistics','integer' label=T('Visits'), default=0))

In the above model I added an imaginary tab property (which will cause en error in real application)  to group  fields with the same tab property under the same tab in a detail smartgrid form.
To solve the order of the tabs, the call to the smartgrid will include a 'tab' property which will be a list with tabs.

Another more flexible solution  could be to add a 'tab ' property to  smartgrid which will be a dictionary with tab names as keys and a lists of  field names as values. This will allow also to order the fields on each tab and will not require any change to the existing define_table structure.

Example:
tabs={'General':['first_name','last_name'],'Advance':['phone'],'Statistics':['visits']}

I have this strange filling that this is already possible, may be something to do with formstyle.
Any help will be appreciated

Anthony

unread,
May 25, 2017, 10:02:08 AM5/25/17
to web2py-users
You could create a custom formstyle function and then pass it to the grid via the "formargs" argument or set response.formstyle.

Anthony

icodk

unread,
May 26, 2017, 9:20:21 AM5/26/17
to web2py-users
Tried to create  a custom formstyle by copying existing one from sqlhtml.py (formstyle_table3cols) to my_formstyle in my controller
and set the grid formstyle=my_formstyle 
but got an error :

<type 'exceptions.RuntimeError'> formstyle not found

I can see that there is a formstyles  Storage in sqlhtml.py  so itryed to add 
formstyles['my_formstyle']='my_formstyle'
just before the call to grid = SQLFORM.smartgrid()
but got:

<type 'exceptions.NameError'> global name 'formstyles' is not defined


Can you please point me to simple example of how to add a custom forstyle ?
Thanks in advance
Reply all
Reply to author
Forward
0 new messages