I use this javascript for my autocompletes...

<script type="text/javascript">
    $(document).ready(function() {
        $( "#no_table_requestor" ).autocomplete({
            source: "{{=URL('contacts','autocomplete')}}",
            select: function(event, ui) {
                $( "#no_table_requestor_id" ).val(ui.item.id);
                $( "#no_table_requestor" ).val(ui.item.label);
                return false;
            }
        });
</script>

The SQLFORM.factory looks like this:

    #  define the fields
    fields = []
    fields.append(Field('requestor', requires=IS_NOT_EMPTY()))
    fields.append(Field('requestor_id','int'))

    #  create the form
    form = SQLFORM.factory(*fields)

    form.element(_name='requestor').update(_class='form-control', 
_placeholder='Requested by')
    form.element(_name='requestor_id').update(_class='hidden')

    if form.process(onvalidation=price_quote_validation).accepted:
        quote = form.vars

...and the lookup code...

@auth.requires_login()
def autocomplete():
    terms = request.vars.term

    queries = [db.contact.id > 0]
    if terms and terms != '':
        queries.append((db.contact.first_name.contains(terms)) |
                       (db.contact.last_name.contains(terms)) |
                       (db.contact.company.contains(terms)))

    query = reduce(lambda a,b:(a&b),queries)

    contacts = []

    for contact in db(query).select(orderby=[db.contact.company, 
db.contact.last_name, db.contact.first_name]):
        if contact.company and contact.company != '':
            if contact.last_name and contact.last_name != '' and 
contact.first_name and contact.first_name != '':
                contacts.append({'label':'%s %s - %s' % 
(contact.first_name, contact.last_name, contact.company), 'id':contact.id})
            else:
                contacts.append({'label':contact.company, 'id':contact.id})
        else:
            contacts.append({'label':'%s %s' % (contact.first_name, 
contact.last_name), 'id':contact.id})

    return response.json(contacts)

Hope that helps....

-Jim

On Tuesday, June 24, 2014 4:06:03 AM UTC-5, Annet wrote:
>
> I've got this form:
>
> form = SQLFORM.factory(
>     Field('tag', length=128, requires=[IS_IN_DB(db, 'tag.name', 
> '%(name)s', error_message='Tag not in database')]),
>     Field('locality', length=64, requires=[IS_IN_DB(db, 'locality.name', 
> '%(name)s',
>                                                     
> error_message='Locality not in database')]),
>     hideerror=True, separator='', formstyle=bootstrap3)
>
> Fields tag and locality are autocomplete fields based on jQueryUI.
> In the view I've got:
>
> <script type="text/javascript">
> $(function() {$("#no_table_tag").autocomplete({source: "{{=URL('jqueryui', 
> 'nodetag_autocomplete')}}",minLength: 2});});
> $(function() {$("#no_table_locality").autocomplete({source: 
> "{{=URL('jqueryui', 'addresslocality_autocomplete')}}",minLength: 2});});
> </script>
>
> And in a controller called jqueryui:
>
> def addresslocality_autocomplete():
>     rows = 
> db(db.address.locality.like(request.vars.term+'%')).select(db.address.locality,
>  
> distinct=True,
>                                                                       
> orderby=db.address.locality).as_list()
>     result = [r['locality'] for r in rows]
>     return response.json(result)
>
> def nodetag_autocomplete():
>     rows = 
> db(db.node_tag.tag.like(request.vars.term+'%')).select(db.node_tag.tag, 
> distinct=True,
>                                                                   
> orderby=db.node_tag.tag).as_list()
>     result = [r['tag'] for r in rows]
>     return response.json(result)
>
> What I want is, make the autocomplete options for field locality depend on 
> the value of field tag.
> Coded with the constant tag 'Personal coaching', something like"
>
> In the view:
>
> $(function() {
>   $("#no_table_locality").autocomplete({source: "{{=URL('jqueryui', 
> 'addresslocality_autocomplete')}} + '/' + 'Personal coaching' ", minLength: 
> 2})
> ;});
>
>
> In the jqueryui controller:
>
> rows = db((db.node_tag.tag=='Personal coaching') & 
> (db.node_tag.nodeID==db.address.nodeID) &
>               
> (db.address.locality.like(request.vars.term+'%'))).select(db.address.locality,
>  
> distinct=True,
>                                                                       
> orderby=db.address.locality).as_list()
>
> In the view I tried:
>
> var x = document.getElementById("no_table_tag").value;
>
> and replace '/' + 'Personal coaching' with '/' + x
>
> but that doesn't work.
>
> In the jqueryui controller this doesn't work either:
>
> (db.node_tag.tag==request.args(0))
>
>
> Is there a way to solve this issue or is it just not possible.
>
>
> Regards,
>
> Annet
>

-- 
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/d/optout.

Reply via email to