Hi Richard

It looks like you have got here faster than I did! Glad it worked out for
you. Just some comments:

Using the version I posted allows one to handle the AJAX callback directly
within the function embedding the widget. Each function can handle the
callback differently. One requirement though is that the widget needs to be
embedded at the function level rather than when the db tables are defined.
Ultimately, which is preferred depends on the use case requirement.
Embedding the widget within a function is done as you have suggested:
def somefunction():
   if request.vars.ac_table_field:
      rows = db(db.addr.zip.like("%s%%" %
request.vars._ac_table_field)).select(limitby=(0,10))
      return dict(options=["%d_%s, %s, %s %s" % (r.id, r.streetnum,
r.streetname, r.city, r.zip) for r in rows])
   kwargs = dict(...)
   db.table.field.widget = lambda field, value: bs_typeahead(field, value,**
kwargs)
   form = SQLFORM(table)
   return dict(form=form)

**if there are no optional arguments, the widget could also be embedded
like so-which would give a marginal performance improvement:
db.table.field.widget = bs_typeahead


For Calvin that inadvertently send this answer only to me and not to the
mailing list.

Richard

On Mon, Mar 18, 2013 at 8:56 PM, Richard Vézina <ml.richard.vez...@gmail.com
> wrote:

> Here what I come up with :
>
> 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})
>     )
>
> 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 })
>
> 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)
>
>
> On Mon, Mar 18, 2013 at 10:10 AM, Richard Vézina <
> ml.richard.vez...@gmail.com> wrote:
>
>> Hello Calvin,
>>
>> This seems interesting. I was planning to work un this when I get time
>> but it looks that what you did is a good start!
>>
>> I have a question, I am not sure how you was thinking to initialize the
>> widget on a select type field??
>>
>> Should I do something like this in order to use your typeahead
>> autocomplete widget? :
>>
>> db.table.field.widget = lambda field, value: bs_typeahead(field, value,
>> ???kwargs)
>>
>> ??
>>
>> Thanks
>>
>> Richard
>>
>>
>>
>>
>> On Mon, Mar 11, 2013 at 4:45 AM, Calvin <calvin....@gmail.com> wrote:
>>
>>> If of interest to anyone, eventually, I made my own autocomplete based
>>> on Bootstrap Typeahead:
>>>
>>> def bs_typeahead(f, v, **kwargs):
>>>     options = {'_data-provide':'textahead'}
>>>     options.update(kwargs)
>>>     return CAT(
>>>         INPUT(_type="text", _class="string", _id="%s_%s_ac" % (f.
>>> _tablename, f.name), **options),
>>>         INPUT(_type="hidden", _id="%s_%s" % (f._tablename, f.name),_name
>>> =f.name, _value=v, requires=f.requires),
>>>         SCRIPT("$($('#%(table)s_%(field)s_ac').typeahead({minLength: 3,
>>> source:function(query,process){console.log(query); return
>>> $.get('%(url)s.json', {'_ac_%(table)s_%(field)s': query},
>>> function(data){return process(data.options)})},
>>> highlighter:function(item){return
>>> item.split('_')[1]},updater:function(item){ var p=item.split('_');
>>> $('#%(table)s_%(field)s').val(p[0]); return p[1]}}))" % {'url':URL(),
>>> 'table':f._tablename, 'field':f.name})
>>>     )
>>>
>>> and with the appropriate handler for request.vars['ac_%s_%s'
>>> %(f._tablename, f.name)]
>>>
>>> --
>>>
>>> ---
>>> 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 web2py+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>
>>
>

-- 

--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to