On Sun, May 22, 2011 at 9:02 AM, 黄祥 <[email protected]> wrote:

> hi,
>
> is it possible to use form in loop view?
>
> e.g.
> {{for i, row in enumerate(rows):}}
>    {{if i == items_per_page: break}}
>
>    {{db.blog_comment.blog_id.default = row.id}}
>    {{form = crud.create(db.blog_comment,
>                         message = T('Record Inserted'))}}
>
>    {{=DIV(form)}}
> {{pass}}
>
> i just can show the form, but when i submit the data is not in
> database blog comment, i've already tried sqlform too but got same
> result.
>
> did anyone know how to fix it?
> any hints, solutions is greatly appreciate, thank you so much before.


Like Massimo wrote and advised against, it is possible like you are
suggesting to create forms on the view. A simple way to keep logic in the
controller is to do something like this (which is what I think you want):

In the controller you pass some you data - for example a set of rows
"contacts"
In the view:
{{for rec in contacts:}}
                <div id="edit_contact_block{{=rec.id}}"
class="info_block_body edit_contact_block" style="display:none">
         {{=edit_contact_form(rec)}}
                </div>
{{pass}}

Please note:
  * each div as a standard name with the contacts id added as sufix
"edit_contact_bock{{=rec.id}}"
  * initial style="display:none" the form is shown by the user using jquery

The forms are created with a function defined in the controller. Here's the
function code:
def edit_contact_form(contact):
    id='edit_contact_form'+str(contact.id)
    form = SQLFORM(db.contact, contact,
                   fields=['contact', 'contact_role', 'value_offered',
'value_closed'],
                   submit_button='Save',
                   showid=False, _id=id,
                   deletable=True,
                   delete_label='Delete:')
    form[0][-1][1].append(INPUT(_type="button", _value='Cancel',
                                _class='cancel_edit_contact',
                                _style="margin-left:3px;"))
    if form.accepts(request.vars, session, formname=id):
         redirect(URL(r=request, f='view_opportunity', args=contact.oppty.id
))
    elif form.errors:
        response.flash='Edition failed!'
    return form

Please note the juju:
    id='edit_contact_form'+str(contact.id)
    ...
    if form.accepts(request.vars, session, formname=id):

Which links each form to a single contact.

Note that some javascript (jquery) is needed to show and hide data and
forms. Since the specific javascript / css to do this is limited to this
specific page (in my app), I append it to response files:

{{response.files.append(URL(r=request,c='static/biz',f='common.js'))}}
{{response.files.append(URL(r=request,c='static',f='view_oppty.css'))}}
{{response.files.append(URL(r=request,c='static/biz',f='view_opportunity.js'))}}

{{extend 'layout.html'}}

I assume that you either show every form or know how to do this in
javascript/jquery.
If anyone as a clearer method to do this I am VERY interested in knowing
about it!

HTH,
Miguel

Reply via email to