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

  • 0
    mrfreeze 4 years ago
    If you are doing the insert from appadmin you will need to change the items_url in the widget to include the controller where you are getting items from: items_url = URL(r=request,f="get_items") ...to... items_url = URL(r=request,c='default',f="get_items") Try that and let me know.

  • 0
    sophie 4 years ago
    I made this, and it work! but i still don't know the difference def insert(): (db, table) = get_table(request) tabla = str(db[table]) exec ("form = SQLFORM(db.%s, ignore_rw=ignore_rw)" %tabla) if form.accepts(request.vars, session): response.flash = T('new record inserted') return dict(form=form)

  • 0
    sophie 4 years ago
    Hi i want to put the files in "static files", i copied into there and in "web2py_ajax.html" i append the files. But it doesnt work, there is something im forgetting, can you help me with this? maybe a manual or something to know what i can do plz

  • 0
    mrfreeze 4 years ago
    If you copied them to the root of 'static', put this in your view:
show more comments

Hosting graciously provided by:
Python Anywhere