Requirements
Download this jQuery multi-select plugin.
Extract the files and put them in a folder called multiselect in your static folder.
Include the plugin in your model.
response.files.append(URL(r=request,c='static/multiselect',f='jquery.multiSelect.js'))
response.files.append(URL(r=request,c='static/multiselect',f='jquery.multiSelect.css'))
Setting up
Place the widget code in your model.
def multiselect_widget(f,v):
import uuid
d_id = "multiselect-" + str(uuid.uuid4())[:8]
wrapper = DIV(_id=d_id)
inp = SQLFORM.widgets.options.widget(f,v)
inp['_multiple'] = 'multiple'
inp['_style'] = 'min-width: %spx;' % (len(f.name) * 20 + 50)
if v:
if not isinstance(v,list): v = str(v).split('|')
opts = inp.elements('option')
for op in opts:
if op['_value'] in v:
op['_selected'] = 'selected'
scr = SCRIPT('jQuery("#%s select").multiSelect({'\
'noneSelected:"Select %ss"});' % (d_id,f.name))
wrapper.append(inp)
wrapper.append(scr)
if request.vars.get(inp['_id']+'[]',None):
var = request.vars[inp['_id']+'[]']
if not isinstance(var,list): var = [var]
request.vars[f.name] = var
del request.vars[inp['_id']+'[]']
return wrapper
Assign the widget to your field.
db.stuff.name.widget = multiselect_widget
Try it out
Build a form with the widget in your controller
def index():
db.stuff.name.requires = IS_IN_SET(['Apples','Oranges','Bananas','Kiwis','Lemons'],multiple=True)
form = SQLFORM(db.stuff)
if form.accepts(request.vars,session):
response.flash = 'Got it'
return dict(form=form)
Shazam!
Comments (4)
0
jonhy 14 years ago
0
mrfreeze 14 years ago
0
jonhy 14 years ago
0
richard 14 years ago