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

In your model:


def ajax_autocomplete(f,v):
    items_url = URL(r=request,f="get_items")
    wrapper = DIV()
    inp = SQLFORM.widgets.string.widget(f,v)
    scr1 = SCRIPT("jQuery('#%s').autocomplete('%s');" % (inp['_id'],items_url),_type="text/javascript")
    return wrapper

db.define_table("widgets", Field("state",widget=ajax_autocomplete))

In your controller:

def index():
    form = SQLFORM(db.widgets)
    if form.accepts(request.vars,session):
        response.flash = "New record added"
    all = db().select(db.widgets.ALL)    
    return dict(form=form,all=all)

def get_items():
    q = request.vars.q
    if q:
        states = ['Alabama','Alaska','American Samoa','Arizona','Arkansas','California','Colorado',
              'Connecticut','Delaware','District of Columbia','Florida','Georgia','Guam','Hawaii',
               'Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota',
               'Northern Marianas Islands','Ohio','Oklahoma','Oregon','Pennsylvania','Puerto Rico',
               'Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia',
               'Virgin Islands','Washington','West Virginia','Wisconsin','Wyoming']

        match = '\n'.join([s for s in states if q.lower() in s.lower()])
        return match
    return ''


Related slices

Comments (12)

  • Login to post

  • 0
    sophie 11 years ago
    Hi i try to put your widget in my project, and i have this ticket. Traceback (most recent call last): File "gluon/restricted.py", line 178, in restricted File "C:/Users/HP/Desktop/web2py/applications/SBC/models/db.py", line 7, in AttributeError: 'NoneType' object has no attribute 'append'

  • 0
    mrfreeze 11 years ago
    You need the latest version of web2py that supports response.files. Try linking the js and css files directly in your view and see if it works.

  • 0
    sophie 11 years ago
    i have to do a function per textbox to autocomplete?

  • 0
    mrfreeze 11 years ago
    @sophie - I'm not sure I understand your question. Are you talking about the function to get items? If so, you can point all of them to the same function and use request.vars.[your input id] to determine which input is requesting data. Hope that makes sense.

  • 0
    sophie 11 years ago
    I am doing the insert with the appadmin and my insert is like this: def insert(): (db, table) = get_table(request) form = SQLFORM(db[table], ignore_rw=ignore_rw) #Here is the problem if form.accepts(request.vars, session): response.flash = T('new record inserted') return dict(form=form) The problem i have, is that the auto-complete don't work with this type of insert. I think that "form = SQLFORM(db[table], ignore_rw=ignore_rw)" is different at "form = SQLFORM(db., ignore_rw=ignore_rw)" Is there a way to solve this? i don't know exactly why they are different
show more comments

Hosting graciously provided by:
Python Anywhere