Component with argument example

52 views
Skip to first unread message

clara

unread,
Dec 14, 2014, 2:54:06 PM12/14/14
to web...@googlegroups.com
Hello,

I would like to see an example of a component with an argument. I tried it with no luck, If I define the component without arguments it works fine but if I add an argument loading the page that has the component leads to some kind of infinite recursion. Any help on this would be appreciated. To be more explicit:

in the controller: 
@auth.requires_login()
def component_action():
    itemid = request.args(0)
    form = SQLFORM.grid(db.address.client==itemid)
    return dict(form=form)

in views:
I create a "component_action.load":

<div class="manage_things2">
{{=form}}
</div>

I use the component in another action adding to the action's HTML:
{{=LOAD('default','component_action.load',args=2, ajax=True)}}

Is there anything awefully wrong in this?

I will appreciate your help. Thanks!

Clara


Anthony

unread,
Dec 14, 2014, 9:43:24 PM12/14/14
to web...@googlegroups.com
The grid makes use of URL args, so if the base URL of the grid action also includes args, you must tell the grid to preserve those extra-grid args via its "args" argument:

form = SQLFORM.grid(db.address.client == itemid, args=request.args[:1])

Anthony

clara

unread,
Dec 15, 2014, 9:25:27 AM12/15/14
to web...@googlegroups.com
Hello Anthony,

Thanks for your reply. I am trying to pass itemid to the component. How do I do that?

Basically imagine a model like>
db.define_table('person',
                
Field('firstname'),
                
Field('lastname'), format='%(firstname)s %(lastname)s')
db
.define_table('dog',
                
Field('name'),
                
Field('owner',db.person))

and what I want to generate is a view where there is every person listed and each person has a grid below with the dogs it owns. I figured components would be the best approach, but I don't know how to pass a parameter (the owner ID, for the example model) to the component. 

Thanks,

Clara

Anthony

unread,
Dec 15, 2014, 10:19:36 AM12/15/14
to web...@googlegroups.com
The way you're doing it (with the adjustment I suggested) should work. Are you still having problems?

Anthony

clara

unread,
Dec 15, 2014, 12:36:16 PM12/15/14
to web...@googlegroups.com
Hello Anthony,

It did work!!! Thanks ! 

Now, something does not work well when adding records into the grid component: I restrict the "owner" so that the record should be appended to the same grid. Now I enter the record, it gets recorded successfully but the grid (in which I added the record) updates to the largest ID (it looks like the last grid in the view). If I refresh the view, everything is in order. Do I need to do something else? Could this be a bug?

I can send the code in if needed. Thanks again for the great support.

Clara

Anthony

unread,
Dec 15, 2014, 12:50:23 PM12/15/14
to web...@googlegroups.com
Probably need to see the code.

Clara Ferrando

unread,
Dec 15, 2014, 1:06:56 PM12/15/14
to web...@googlegroups.com
Hello Anthony,

Thanks for your patience :)

Here is the code:

MODEL

db.define_table('t_owner',
    Field('f_name', type='string', label=T('Name')), 
    Field('f_lastname', type='string', label=T('Lastname')),auth.signature,
    format='%(f_name)s',
    migrate=settings.migrate)

db.define_table('t_pet',
    Field('f_name', type='string', label=T('Name')),
    Field('f_animal', type='string', label=T('Animal')),
    Field('f_breed', type='string', label=T('Breed')),
    Field('f_owner', type='reference t_owner', label=T('Owner')),
    auth.signature,
    format='%(f_name)s',
    migrate=settings.migrate)

COMPONENT ACTION
@auth.requires_login()
def pet_component():
itemid = request.args[0]
q = db.t_pet.f_owner == itemid
db.t_pet.f_owner.default = itemid
db.t_pet.f_owner.writable = False
form = SQLFORM.grid(q,args=request.args[:1],csv=False)
return locals()

pet_component.load : 
<div class="myclass">
{{=form}}
</div>

ACTION THAT USES THE COMPONENT:
@auth.requires_login()
def all_manage():
owner_forms =[]
ids = []
qowner = db.t_owner.id>0
owners = db(qowner).select()
for i,el in enumerate(owners):
owner_forms.append(SQLFORM.grid(db.t_owner.id==el.id, fields=[db.t_owner.f_name,db.t_owner.f_lastname],sortable=False,details=False,create=False, searchable=False,csv=False,deletable=False,editable=False))
ids.append(el.id)
return locals()

all_manage.html view:
{{extend 'layout.html'}}

<h2>All Manage</h2><p>
{{for i,el in enumerate(owner_forms):}}
{{=el}}
{{ids[i]}}
{{=LOAD('default','pet_component.load',args=ids[i], ajax=True)}}
{{pass}}

Let me know if you find something. Thanks a lot.

Clara




--
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/duKdi15aRcE/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/d/optout.

clara

unread,
Dec 15, 2014, 6:53:31 PM12/15/14
to web...@googlegroups.com
Anthony,

I think I found the problem. In page 405 of the book's 5th edition it says:

"Because of the way grid works one can only have one grid per
controller function, unless they are embedded as components via
LOAD. To make the default search grid work in more than one
LOADed grid, please use a different formname for each one."

I changed the component and added a unique formname to it as follows:

@auth.requires_login()
def pet_component():
itemid = request.args[0]
q = db.t_pet.f_owner == itemid
db.t_pet.f_owner.default = itemid
db.t_pet.f_owner.writable = False
form = SQLFORM.grid(q,args=request.args[:1],csv=False,formname = "form"+str(itemid))
return locals()

And this solved the problem!  Now when I add a record to any of the grids and save it, the grid reloads filtering by owner and and the new record is shown .

Thanks for the great support!

Clara
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages