pre-populate form using selected record in SQLFORM.grid

669 views
Skip to first unread message

John Philip

unread,
Feb 11, 2014, 11:51:29 AM2/11/14
to web...@googlegroups.com
Hi there,

I am a newbie to web2py. I did try to look for similar threads but couldn't find a solution specifically for my problem. I want to pre-populate a form from a current record in the SQLFORM.grid. 

addpass = [lambda project: A('add pass', _class="btn", _href=URL("default","addpass"))]
# added a link button to open form addpass which should have pre-populated fields in sgrid shown below.

 sgrid = SQLFORM.grid(db.nextpasses, links=addpass, create=False, editable= False, deletable=False, fields=[db.nextpasses.DOY,db.nextpasses.STATION,db.nextpasses.BOT,db.nextpasses.EOT], maxtextlength=50,headers={'nextpasses.DOY' : 'DOY','nextpasses.STATION' : 'Station', 'nextpasses.BOT' : 'Aos', 'nextpasses.EOT' : 'Los'})

Any help would be greatly appreciated!

many thanks and regards,

John Philip

Jim S

unread,
Feb 11, 2014, 4:16:28 PM2/11/14
to web...@googlegroups.com
Try setting the defaults on the respective fields:

db.nextpasses.field_name.default = default_value_for_this_field

-Jim

John Philip

unread,
Feb 12, 2014, 5:37:59 AM2/12/14
to web...@googlegroups.com
Hi Jim, thanks for you reply. Sorry I guess I should be more specific. I have a SQLGRID form (displaying contents of table db.nextpasses) , In the SQLFORM.grid I have inserted a button called addpass so for every record in the SQLgrid I have a button addpass. When clicking "addpass" another form "addpass" is opened which should be pre-populated from the record values in the SQLform.grid in the previous form.

maybe something like this

 sgrid = SQLFORM.grid(db.nextpasses, links=addpass(db.addpass.field_name.default = db.nextpasses.field_name

I am bit confused where I need to pass the values when initiating the lamba project addpass.

thanks,
John

Jim Steil

unread,
Feb 12, 2014, 9:19:36 AM2/12/14
to web...@googlegroups.com
So, with this statement:

addpass = [lambda project: A('add pass', _class="btn", _href=URL("default","addpass"))]

You're creating a link to an entirely different controller, right?

If that is correct then I would pass the parameters for that 'project' as vars on the URL command as such:

addpass = [lambda project: A('add pass', _class="btn", _href=URL("default","addpass",vars={'DOY':project.DOY, 'STATION':project.STATION, 'BOT':project.BOT, 'EOT':project.EOT}))]

...assuming you know all your values you wish to pass when the button is being created.

Then in default/addpass use this:

db.nextpasses.DOY.default = request.get_vars.DOY
db.nextpasses.STATION.default = request.get_vars.STATION
db.nextpasses.BOT.default = request.get_vars.BOT
db.nextpasses.EOT.default = request.get_vars.EOT

Not sure if this is what you're after or if this is even the best solution.  But, may spark some thoughts in others on the list too.

Hope it helps.

-Jim



--
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/-bzBmvCsYyQ/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.

Anthony

unread,
Feb 12, 2014, 9:40:27 AM2/12/14
to web...@googlegroups.com
Do you want to actually update the record in question, or simply insert a completely new record that is prepopulated with the values from this record? If the former, then just pass the record ID in the URL and pass that ID to SQLFORM in order to create an update form.

Anthony

John Philip

unread,
Feb 13, 2014, 2:47:40 AM2/13/14
to web...@googlegroups.com
Hi Anthony and Jim,

thanks very much for your help. I got it working with Jim suggestion since I only wanted to insert a completely new record with the prepopulated values from the previous sqlgrid form.

thanks again!

regards,

John
Reply all
Reply to author
Forward
0 new messages