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