check boxes with is_in_set

70 views
Skip to first unread message

R U

unread,
Dec 8, 2016, 4:44:09 PM12/8/16
to web...@googlegroups.com
hi all,

I understand this might be too much in the realm of "please write my code for me" with that in mind let me ask.
Is it possible to use -- is_in_set -- to create a list of check boxes instead of a drop-down? 

what I am doing is: two types of users.  both have access to the same list.  user type 1) can choose any and all of the items; user type 2) can only choose 1.

I employ a drop down and is_in_set for the second type of user and that is exactly what I am looking for.
Field('yo', requires=IS_IN_SET(['fl', 'ch',  'Fo',  'Mo',  'Ma', 'Ca', 'Foo', 'Mai', ...........]))

The other I have a bunch of boolean fields such as.  

    Field('fl', 'boolean'),
    Field('ch', 'boolean'),
    Field('Fo', 'boolean'),
    Field('Mo', 'boolean'),
    Field('Ma', 'boolean'),
    Field('Ca', 'boolean'),
    Field('Foo', 'boolean'),
    Field('Mai', 'boolean'),
    Field................................

I would rather do this similar to the prior example, i.e. using a list.  I think it would give me greater control over styling in the view and other iterations I am planning on doing.  And I would like to keep originating this from the model because all the model has to offer. 

Thank you

Rob 

Jim Russell

unread,
Dec 8, 2016, 5:16:46 PM12/8/16
to web2py-users
Rob,

I used this code to get a list with checkboxes.

    form=SQLFORM.factory(
        Field('hostnames',"list:string", default=hostnames,widget=SQLFORM.widgets.checkboxes.widget,requires=[IS_IN_SET(hostnames,multiple=True),IS_NOT_EMPTY()]))

hostnames is a list containing strings.

黄祥

unread,
Dec 12, 2016, 4:50:58 PM12/12/16
to web2py-users
trying the tricks but got error when the checkbox is empty (1 or all checkbox is empty, when all checkbox is on its work) :
e.g.
def test_form():
redirect_url = 'test'

list_chart = [(True, 'line'), 
 (True, 'pie'), 
 (True, 'bar') ]

form = SQLFORM.factory(
Field('chart', 'list:string', 
 default = list_chart,
 widget = SQLFORM.widgets.checkboxes.widget, 
 requires = [IS_IN_SET(list_chart, multiple = True) ] ) )
if form.process().accepted:
chart = form.vars.chart
line = form.vars.line[0]
pie = form.vars.pie[1]
bar = form.vars.bar[2]

response.new_window = URL(redirect_url, args = [chart] )
#response.new_window = URL(redirect_url, args = [line, pie, bar] )
elif form.errors:
response.flash = T('Form has errors')
return dict(form = form)

Traceback error:
line = form.vars.chart[0]
IndexError: list index out of range

any idea how to face it using web2py way?

thanks and best regards,
stifan

Jim Russell

unread,
Dec 13, 2016, 2:53:52 AM12/13/16
to web2py-users
In your example, list_chart is not a list of strings, it is a list of tuples.

In my code, hostnames is just a list of hostnames.  e.g. ('hostname1.example.com','hostname2.example.com')

I assume that you are trying to make the checkboxes default to true. For me, they defaulted to being checked.

黄祥

unread,
Dec 13, 2016, 5:40:47 AM12/13/16
to web2py-users
it's work now, even in the tuple inside the list data type, my bad to treat the list data type in an unproper manner, hehe
is_in_set list checkboxes refer to rows table database 
e.g.
def attendance_form():
target_response = "jQuery('#attendance_checkout').get(0).reload()"

rows_list_employee = db(db.employee.id > 0).iterselect()
list_employee = []
for row_list_employee in rows_list_employee:
data_employee = (row_list_employee.id, '%s' % (row_list_employee.name) )
list_employee.append(data_employee)

form = SQLFORM.factory(
Field('employee', 'list:string', 
 widget = SQLFORM.widgets.checkboxes.widget, 
 requires = [IS_IN_SET(list_employee, multiple = True), IS_NOT_EMPTY() ] ), 
)
if form.process(formname = 'form_detail').accepted:
response.flash = T('Form accepted')
for row_id in request.vars.employee:
id = int(row_id)
row_employee = db(db.employee.id == id).iterselect().first()

query_attendance = ((db.attendance.employee == row_employee.id) & 
(db.attendance.attendance_date == request.now) )
row_attendance = db(query_attendance).iterselect().first()

if row_attendance:
response.flash = T("Employee already insert today")
else:
basic_salary = int(row_employee.basic_salary)
meal = int(row_employee.meal)
transport = int(row_employee.transport)

total = basic_salary + meal + transport

session_attendance[id] = basic_salary, meal, transport, total
response.js =  target_response
elif form.errors:
response.flash = T('Form has errors')
return dict(form = form)

is_in_set list checkboxes for view report (not related with database table) 
e.g.
def product_form():
redirect_url = 'report_product'

list_show_product = [('chart', T('Chart') ), 
('account', T('Account') ) ]

form = SQLFORM.factory(
Field("product", "reference product", 
 requires = IS_IN_DB(db, db.product.id, db.product._format, 
   zero = T('Choose One') ) ),
Field('show', 'list:string', 
 widget = SQLFORM.widgets.checkboxes.widget, 
 requires = IS_IN_SET(list_show_product, multiple = True) ) )
if form.process().accepted:
product = form.vars.product
show = form.vars.show
show.insert(0, product)

response.new_window = URL(redirect_url, args = show)
elif form.errors:
response.flash = T('Form has errors')
return dict(form = form)

best regards,
stifan
Reply all
Reply to author
Forward
0 new messages