pre selected checkboxes

757 kali dilihat
Langsung ke pesan pertama yang belum dibaca

Fabiano Faver

belum dibaca,
10 Jul 2012, 14.39.4410/07/12
kepadaweb...@googlegroups.com
I want to creat something similar to SQLFORM(auth_membership) but I want the user to choose a group and all users show up as checkboxes and users who already belong to this group are checked as well.

I`m downt know how to pre-select these auth_users.

any tips?

Richard Vézina

belum dibaca,
10 Jul 2012, 15.46.4810/07/12
kepadaweb...@googlegroups.com
in your model Field(..., default=True) or in the controller db.table.field.default=True

You also need to use checkbox widget.

Richard

Fabiano Faver

belum dibaca,
10 Jul 2012, 16.03.5810/07/12
kepadaweb...@googlegroups.com
Won`t it apply True to all of them? 

this is what I have now with sqlform:
Field('users', requires = IS_IN_DB(db, db.auth_user, '%(email)s', multiple=True), widget=SQLFORM.widgets.checkboxes.widget )

I imagined there were a argument using  lambda that would set 'checked'  for each checkbox  that match the lambda, something like:

Field('users', requires = IS_IN_DB(db, db.auth_user, '%(email)s', multiple=True), widget=SQLFORM.widgets.checkboxes.widget, test_checked= lambda row: test_to_set_checked_for_each_checkbox )

I'm trying to use 2 FORMs now..

Richard Vézina

belum dibaca,
10 Jul 2012, 16.10.3810/07/12
kepadaweb...@googlegroups.com
default=lambda row: row.check_box_field

Richard

Fabiano Faver

belum dibaca,
10 Jul 2012, 16.49.4010/07/12
kepadaweb...@googlegroups.com
controller:

def gerenciar_grupos():
form_not_usu = SQLFORM.factory(
Field('usuarios', requires=IS_IN_DB(db, db.auth_user,'%(email)s',multiple=True),widget=SQLFORM.widgets.checkboxes.widget, default=True),
                                )
return dict(  form_not_usu = form_not_usu)


view:

{{extend 'layout.html'}}
{{=form_not_usu.custom.begin}}

<ul>
{{for x in form_not_usu.custom.widget.usuarios:}}
{{=LI(x)}}

{{pass}}
</ul>

{{=form_not_usu.custom.submit}}
{{=form_not_usu.custom.end}}



Checkboxes are not checked when accessing the page.

Em terça-feira, 10 de julho de 2012 16h46min48s UTC-3, Richard escreveu:

villas

belum dibaca,
11 Jul 2012, 06.35.4611/07/12
kepadaweb...@googlegroups.com
Don't you need a list field type for 'usuarios'?

Fabiano Faver

belum dibaca,
11 Jul 2012, 13.50.4911/07/12
kepadaweb...@googlegroups.com
the field is shown if I dont declare the type, however the checks are not checked with default=True, like I wrote

Richard Vézina

belum dibaca,
11 Jul 2012, 15.52.3311/07/12
kepadaweb...@googlegroups.com
Fabiano,

I think there is an issue with widget=SQLFORM.widgets.checkboxes.widget do you use it?

I try without widget=SQLFORM.widgets.checkboxes.widget and I can set default=True and I get a checked box, but if I use it the box doesn't get checked.

If you don't need the fancy feature of widget (I think that it is mostly to have many checkboxes for the same field and specify labels for those multiple checkboxes) avoid using it could solve your issue.

I will investigate further why and how SQLFORM.widgets.checkboxes.widget could be broken.

Richard

Richard Vézina

belum dibaca,
11 Jul 2012, 16.19.5411/07/12
kepadaweb...@googlegroups.com
Ok Fabiano, no issue with SQLFORM.widgets.checkboxes.widget...

I didn't know exactly how it works :)

In this thread look for Bruno's answers that how I figure out how to pass the proper default to widget :

Basically, since there is multiple checkboxes you need to define a IS_IN_SET requires with your options set (your check boxes) and then you can define a default by passing to default the label of what you use as label for the differents checkboxe in your defined set.

Here a example :

    form = SQLFORM.factory(
        Field('test', type='boolean', requires=IS_IN_SET([1,2,3], multiple=True), widget=SQLFORM.widgets.checkboxes.widget, default = 3),

I pass to default "3" since that the box that I want to be ckecked by default. 

In order to pass more then one choice you just pass a list to default like this :

    form = SQLFORM.factory(
        Field('test', type='boolean', requires=IS_IN_SET([1,2,3], multiple=True), widget=SQLFORM.widgets.checkboxes.widget, default = [2,3]),
        formstyle='divs'
        )

Hope it helps...

I appreciate answer your question since it helps me understand something I will have to cover in the coming days for a particular form.

Ciao!

Richard
Balas ke semua
Balas ke penulis
Teruskan
0 pesan baru