Autocomplete widget filter values

63 views
Skip to first unread message

Armando Hernandez

unread,
Sep 27, 2018, 2:07:31 AM9/27/18
to web2py-users
How can i filter tha value of the autocomplete values of a SQLFORM.widgets.autocomplete(request,db.person.name)

Lets say you only want the autocomplete widget to display person names of persons with ages beetwen 30 to 35 years old

Something like :
Filter=(db.person.age>=30) & (db.person.age<=35) ?

Thanks

Anthony

unread,
Sep 27, 2018, 6:59:45 PM9/27/18
to web2py-users
I haven't tried it, but try:

SQLFORM.widgets.autocomplete(request,db.person.name, db=db(Filter))

db(Filter) returns a Set object, which is callable, just like db() -- so additional queries can be added by the widget.

Anthony

Annet

unread,
May 5, 2020, 10:32:32 AM5/5/20
to web2py-users
Hi Anthony,

I am rebuilding an application that uses the jQuery UI autocomplete widget.

In the view I have the following code:

<script type="text/javascript">
$(function() {$("#no_table_name").autocomplete({source: "{{=URL('jquery', 'name_autocomplete')}}", minLength: 2});});
</script>

name_autocomplete is a function in the jquery controller:

def name_autocomplete():
    left = [db.app_settings.on(db.vtx_vertex.id==db.app_settings.vertexID)]
    rows = db((db.vtx_vertex.name.contains(request.get_vars.term, case_sensitive=False)) &
              ((db.vtx_vertex.typeID.belongs(ORGANIZATIONID, IOTHINGID)) |
              ((db.vtx_vertex.typeID==PERSONID) & (db.app_settings.viewtypeID>ZEROID) &
               (db.app_settings.openID==PUBLIC)))).select(db.vtx_vertex.name, left=left, distinct=True,
                                                          orderby=db.vtx_vertex.name).as_list()

    result = [r['name'] for r in rows]
    return response.json(result)


I'd like to use SQLFORM.widgets.autocomplete(request, db.vtx_vertex.name, limitby=(0,10), min_length=2, db=db(Filter))


I don't know how to code the filter to give me the same list of values.

Is it possible for Filter to call name_autocomplete() in the jquery controller.



Kind regards,

Annet
Reply all
Reply to author
Forward
0 new messages