Hello,
Wrote this and it may be handy for others in the meantime there is a better
integration of autocomplete bootstrap typeahead :
# Widget function
def autocomplete_typeahead_widget(f, v, **kwargs):
table_name = f._tablename
field_name = f.name
field_requires = f.requires
field_value = v
options = {'_data-provide':'typeahead', '_autocomplete': 'off'}
options.update(kwargs)
return CAT(
INPUT(_type="text", _class="string", _id="%s_%s_ac" % (table_name,
field_name), **options),
INPUT(_type="hidden", _id="%s_%s" % (table_name, field_name),
_name=field_name, _value=field_value, requires=field_requires),
SCRIPT("""$(function() {
$('#%(table_name)s_%(field_name)s_ac').typeahead(
{ source: function (query, process) {
$.get('%(url)s.json',
{ q: query },
function (data) {
labels = []
mapped = {}
$.each(data.options, function (i, item) {
mapped[item.option] = item.id
labels.push(item.option)
})
process(labels)
}
)
},
updater:function(item){
$('#%(table_name)s_%(field_name)s').val(mapped[item]); return item}
})
});""" % {'url':URL(c='test',
f='autocomplete_typeahead_widget_json_feed'), 'table_name':table_name,
'field_name':field_name})
)
# Generating JSON function
def autocomplete_typeahead_widget_json_feed():
rows=db(db.table.field>0).select()
options = []
for i,row in enumerate(rows):
options.append({'option': row.field, 'id': row.id})
from gluon.contrib import simplejson
return simplejson.dumps({'options': options })
# Init of the widget
db.other_table.select_field.widget = autocomplete_typeahead_widget
or
db.other_table.select_field.widget = lambda field, value:
autocomplete_typeahead_widget(field, value, MORE_ARGS)
Enjoy!
Richard
--
---
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.