Good day days guys,
I implemented the following code below to enable auto-complete in a text
field, but it doesn't work, can someone please tell me where I am going
wrong;
# Model Files #
db.define_table(
'location',
Field('street_address', type='string',
label=T('Street Address')),
Field('city', type='string',
label=T('City/Town')),
Field('state', type='string',
label=T('State')),
Field('postal_code', type='string',
label=T('Postal Code')),
Field('country', type='string',
label=T('Country')),
Field('latitude', type='string',
writable=False,
label=T('Latitude')),
Field('longitude', type='string',
writable=False,
label=T('Longitude')))
# Autocomplete Function
response.files.append(URL(r=request,c='static/jquery',f=
'jquery.autocomplete.js'))
response.files.append(URL(r=request,c='static/css',f=
'jquery.autocomplete.css'))
def autocomplete_widget(field,value):
import uuid
d_id = "autocomplete-" + str(uuid.uuid4())[:8]
wrapper = DIV(_id=d_id)
inp = SQLFORM.widgets.string.widget(field,value)
rows = field._db(field._table['id']>0).select(field,distinct=True)
items = [str(t[field.name]) for t in rows]
scr = SCRIPT('var data= "%s".split("|");jQuery("#%s
input").autocomplete(data);' % ("|".join(items),d_id))
wrapper.append(inp)
wrapper.append(scr)
return wrapper
db.location.street_address.widget = autocomplete_widget
#Controller file#
def get_items():
MINCHARS = 2
MAXITEMS = 20
query = request.vars.q
fieldname = request.vars.field
tablename = request.vars.table
if len(query.strip()) > MINCHARS and fieldname and tablename:
field = db[tablename][fieldname]
rows = db(field.upper().startswith(query)).select(field,distinct=
True,limitby=(0,MINITEMS))
items = [str(row[fieldname]) for row in rows]
else:
items = []
return '\n'.join(items)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.