If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!


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.


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))
    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)



Related slices

Comments (4)

  • Login to post

  • 0
    jonhy 14 years ago
    My precedent problem have been resolved by the maling list. Now I got "value not in database" helpers message when I select more then one entry of the dropbox... The problem is coming from the web2py recent update 1.80.1, 1.81.4. The same app with 1.79.2 no problem (I package app from 1.80.1 and install it in 1.79.2). Here my function code : db.atable.field2.widget=multiselect_widget db.atable.field2.requires=\ IS_IN_DB(db,'auth_user.id','%(first_name)s %(last_name)s (%(id)s)',multiple=True) form = crud.create(db.atable) Jonhy

  • 0
    mrfreeze 14 years ago
    @jonhy - can you show me your model with the validators?

  • 0
    jonhy 14 years ago
    Got this error in appadmin with the plugin when trying to edit a record : Error traceback Traceback (most recent call last): File "/web2py/gluon/restricted.py", line 178, in restricted exec ccode in environment File "/web2py/applications/app/controllers/appadmin.py", line 410, in File "/web2py/gluon/globals.py", line 96, in self._caller = lambda f: f() File "/web2py/applications/app/controllers/appadmin.py", line 273, in update f='download', args=request.args[:1])) File "/web2py/gluon/sqlhtml.py", line 688, in __init__ inp = field.widget(field, default) File "/web2py/applications/app/models/plugins.py", line 11, in multiselect_widget inp = SQLFORM.widgets.options.widget(f,v) File "/web2py/gluon/sqlhtml.py", line 190, in widget % field SyntaxError: widget cannot determine options of atable.field1 What can I do Jonhy

  • 0
    richard 14 years ago
    This works as advertised. I'm using it to set multiple auth_group memberships all at once in auth_membership

Hosting graciously provided by:
Python Anywhere