I had a quick look at the slice, am I right to conclude that contrary
to the pengoworks autocomplete this one is more generic. When I use
the Pengoworks autocomplete in my model I have to define:

db.address.city.widget=lambda
self,value:INPUT(_type='text',_id='city',_class='ac_input',_name='city',requires=self.requires)

... in web2py_ajax.html:

jQuery('#city').autocomplete('/init/handlers/cityAC',{maxItemsToShow:
12});

... in a controller called handlers:

def cityAC():
    q=''
    if request.vars:
        q=request.vars.q
    if not q:
        return q
    rows=db(db.city.name.like('%s%
%'%q.capitalize())).select(db.city.name,orderby=db.city.name)
    r=''
    for row in rows:
        r='%s%s\n'%(r,row.name)
    return r

... and I have to repeat this for every field I need autocomplete
functionality for.


When I use the one described in the slice, I would put this in db.py:

def autocomplete_widget(f,v):
    import uuid
    d_id = "autocomplete-" + str(uuid.uuid4())[:8]
    wrapper = DIV(_id=d_id)
    inp = SQLFORM.widgets.string.widget(f,v)
    rows = f._db(f._table['id']>0).select(f,distinct=True)
    itms = [str(t[f.name]) for t in rows]
    scr = SCRIPT('var data= "%s".split("|");jQuery("#%s
input").autocomplete(data);' % \
                  ("|".join(itms),d_id))
    wrapper.append(inp)
    wrapper.append(scr)
    return wrapper

... which would work both on my laptop and on the webfaction servers!?

... put this in the handlers controller:

def get_items():
    itms = []
    if request.vars.q and \
       request.vars.table and \
       request.vars.field:
        q = request.vars.q
        f = request.vars.field
        t = request.vars.table
        fld = db[t][f]
        rows = db(fld.upper().like(q.upper()
+"%")).select(fld,distinct=True)
        itms = [str(t[f]) for t in rows]
    return '\n'.join(itms)


... and put this in the table definition:

db.address.city.widget=autocomplete_widget


Is that right?


Kind regards,

Annet.

Reply via email to