Hi mdipierro,

Thanks for the answer!  I have some questions.

1) using the SQLFORM syntax with list comprehension below, how do I
add additional fields?  I need to add 'hidden' fields that match the
name of the 'checkbox' fields - otherwise, non-checked items don't get
put in request.vars and I don't want to assume they are false (ie:
Pagination)

2) Since we're building the form with values from the DB in the
beginning of the function, then updating the DB, then returning the
form, the view gets a stale copy of the form.  I guess you could solve
this with a redirect if form.accepts() is true?  Seems hackish...

Thanks for the help :)

On Jul 9, 5:44 am, mdipierro <[email protected]> wrote:
> This should work:
>
> def index():
>     items = db().select(db.Item.ALL)
>     form=SQLFORM.factory(*[Field('need_%s'%item.id, default=item.need)
> for item in items])
>     if form.accepts(request.vars,session):
>        for item in items:
>           need = not request.vars['need_%s'%item.id]==None
>           item.update_record(need=need)
>    return dict(form=form)
>
> On 9 Lug, 01:04, Rob <[email protected]> wrote:
>
>
>
> > Hi All,
>
> > Sorry for the noob post, but I'm having an issue.  I'm trying to do a
> > little toy 'shopping list' app - the index page is to have a list of
> > 'items' on the page with a 'need' checkbox and an 'update' button.
> > When the update button is pressed, the database is to be updated with
> > the new checked values.
>
> > here is the db:
>
> > db.define_table('Item',
> >     Field('description'),
> >     Field('need', 'boolean')
>
> > here is the controller:
> > def index():
> >     table = TABLE()
> >     items = db().select(db.Item.ALL)
> >     for item in items:
> >         chk = INPUT(_type='checkbox', _name='need_%s' % item.id,
> > value=item.need)
> >         table.append(TR(item.id,item.description,chk))
> >     form = FORM(table,INPUT(_type='submit'))
> >     if form.accepts(request.vars,session):
> >         for k,v in request.vars.items():
> >             if k.startswith('need_'):
> >                 id = k.split('_')[-1]
> >                 item = db(db.Item.id == int(id)).select()[0]
> >                 item.update_record(need=(form.vars[k]=='on'))
> >     return dict(form=form)
>
> > view: default/index.html
> > {{extend 'layout.html'}}
> > {{=form}}
>
> > This doesn't quite work right - it's close and it feels like a
> > kludge.  I need to set the checkbox value to the proper 'request'
> > variable, but I don't process those until the end of the function...
> > am I on the right track here?  I found most of this code on a forum
> > somewhere.
>
> > Thanks for the help... it sucks just starting out!
>
> > -Rob

Reply via email to