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:

response.files.append("http://www.pengoworks.com/workshop/jquery/lib/jquery.autocomplete.js")
response.files.append("http://www.pengoworks.com/workshop/jquery/lib/jquery.autocomplete.css")

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")
    wrapper.components.extend([inp,scr1])
    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',
               'Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland',
               'Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska',
               '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 ''

autocomplete

Related slices

Comments (12)

  • Login to post



  • 0
    sophie 15 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 15 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 15 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 15 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 15 years ago
    If you copied them to the root of 'static', put this in your view:

  • 0
    mrfreeze 15 years ago
    Foiled by my own devices! The code was sanitized. I would just create the link and script tags in your layout.html or view directly (not web2py_ajax.html). There have been a lot of changes to web2py_ajax.html and I think it is recommended to not alter it.

  • 0
    sophie 15 years ago
    I dont know what you mean by that, what i have to put in the tags the files? I try this, copy this files in the "static folder" http://www.pengoworks.com/workshop/jquery/lib/jquery.autocomplete.js http://www.pengoworks.com/workshop/jquery/lib/jquery.autocomplete.css because in the place im going to put my work dont have internet. When i copy this in "web2py_ajax.html" i put this code response.files.insert(3,URL(r=request,c='static',f='autocomplete.js')) response.files.insert(3,URL(r=request,c='static',f='autocomplete.css')) When i tried to run my project, the autocomplete stop working.

  • 0
    mrfreeze 15 years ago
    I mean instead of using response.files to include the js and css, link them directly by using SCRIPT and LINK helpers.

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

  • 0
    mrfreeze 15 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 15 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 15 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.

Hosting graciously provided by:
Python Anywhere