Thanks Denes, this was an interesting exercise and it is good that you
are giving us the benefit of this knowledge.  I suppose like many,  I
thought about the challenge,  but couldn't figure it out.

On Apr 26, 7:49 pm, DenesL <denes1...@yahoo.ca> wrote:
> Since I have not received any answers here is how to do it.
>
> The problem is that to create the links as requested you need access
> to the whole subheading record, and the internal processing of crud
> and SQLTABLE only gives you access to the value of one of the fields
> at a time.
>
> So how can you get to all values in a record?.
>
> Using virtual fields, by adding these to the MODEL e.g.:
>
> class HeadSubs:
>   def linksub(self):
>     return A(self.heading.title,
> _href=URL('list_subheadings',args=[self.heading.id]))
>
> db.heading.virtualfields.append(HeadSubs())
>
> so now table 'heading' has a 'linksub' virtual field.
> Note how you can use any field from the same row to build the virtual
> one.
>
> Following one of Massimo's recent videos the CONTROLLER looks like
> this:
>
> def reload(target): # note1
>   def js(form):
>     response.js='web2py_component("%s","%s")' %(URL(target),target)
>   return js
>
> def index():
>   return dict()
>
> def list_headings():
>   return dict(
>     form=crud.select(
>       db.heading,
>       orderby=db.heading.sequence,
>       columns=['heading.linksub'] ) # note2
>     or 'no headings')
>
> def add_heading():
>   return dict(
>     form=crud.create(
>       db.heading,
>       onaccept=reload('list_headings'))) # note3
>
> note1: to understand how this works look up "python nested functions",
> but it is akin to a closure.
>
> note2: with 'columns' we select only the virtual field which has the
> link in the requested format.
>
> note3: this will force a reload of 'list_headings' after a sucessfull
> insertion.
>
> To complete the use of components, the VIEW:
>
> {{extend 'layout.html'}}
> <h2>Headings</h2>
> {{=LOAD(request.app, 'list_headings.load', ajax=True,
> target='list_headings')}} # note4
> <h3>Add heading</h3>
> {{=LOAD(request.app, 'add_heading.load', ajax=True)}}
>
> note4: the target is what ties the reload code near note3.
>
> The use of virtual fields could be further expanded, for example
> creating elements that have onclick using web2py_component to load
> other parts.
>
> Hope you find it useful.

Reply via email to