autocomplete widget

470 views
Skip to first unread message

Pierre

unread,
Feb 2, 2016, 2:21:58 PM2/2/16
to web2py-users

Is it only ONE autocomplete widget allowed per table ?

this code uses 2 widgets but the second autocomplete field is never filled :

db.define_table('person',
                Field('name', unique=True, requires=IS_NOT_IN_DB(db,'person.name')),
                Field('country'),
                Field('age','integer'))

db.define_table('thing',
                Field('name'),
                Field('persona'),
                Field('personb'))

db.thing.persona.widget = SQLFORM.widgets.autocomplete(request, db.person.name, id_field=db.person.id)
db.thing.personb.widget = SQLFORM.widgets.autocomplete(request, db.person.name, id_field=db.person.id)

Richard Vézina

unread,
Feb 2, 2016, 2:25:44 PM2/2/16
to web2py-users
This should be a name or id html field issue...

Can you have a look at the HTML and tell us what is the name and id attributes values of these two widget?

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

Richard Vézina

unread,
Feb 2, 2016, 2:27:28 PM2/2/16
to web2py-users
And does the second widget change the selected value of the first one ?

Richard

Richard Vézina

unread,
Feb 2, 2016, 2:30:44 PM2/2/16
to web2py-users

Richard Vézina

unread,
Feb 2, 2016, 2:33:49 PM2/2/16
to web2py-users
Is the models you show are the exact model definition you test?

You did not define the persona and personb type which is 'reference person', which may cause the issue...

Richard

Pierre

unread,
Feb 2, 2016, 2:56:55 PM2/2/16
to web2py-users

No the second widget doesn't change the first widget selection

here is the html :


<input id="thing_persona" class="string" type="text" onkeyup="jQuery('#_autocomplete_person_name_auto').val('');var e=event.which?event.which:event.keyCode; function F_autocomplete_person_name(){jQuery('#thing_persona').val(jQuery('#_autocomplete_person_name   etc.....


<input id="thing_personb" class="string" type="text" onkeyup="jQuery('#_autocomplete_person_name_auto').val('');var e=event.which?event.which:event.keyCode; function F_autocomplete_person_name(){jQuery('#thing_personb').val(jQuery('#_autocomplete_person_name etc.....

Pierre

unread,
Feb 2, 2016, 3:10:17 PM2/2/16
to web2py-users
In fact Yes


the second input replaces the first :

personb replaces persona and personb ends up empty

Richard Vézina

unread,
Feb 2, 2016, 3:29:35 PM2/2/16
to web2py-users
Did you try to set the type to these fields? 

db.define_table('thing',
                Field('name'),
                Field('persona', 'reference person'),
                Field('personb', 'reference person'))

--

Richard Vézina

unread,
Feb 2, 2016, 3:31:56 PM2/2/16
to web2py-users
Pierre,

You crop the interresting part... after the etc....

The error is surely coming from the jQuery which for some reason don't get generated properly by the widget...

Richard

Richard Vézina

unread,
Feb 2, 2016, 3:32:31 PM2/2/16
to web2py-users
If you can pack a dummy app to help us reproduce the problem it could help...

Richard

Richard Vézina

unread,
Feb 2, 2016, 3:55:04 PM2/2/16
to web2py-users
Error come from : https://github.com/web2py/web2py/blob/63bb4a7e8a39f466f6269e2f98c7b741d001def9/gluon/sqlhtml.py#L648


The widget can't use the same field name...

There is already a ticket on github about that : https://github.com/web2py/web2py/issues/940


Can be reproduce with this code :

# model
db.define_table('person',
                Field('name', unique=True, requires=IS_NOT_IN_DB(db,'person.name')),
                Field('country', 'string'),
                Field('age', 'integer'))

db.define_table('thing',
                Field('name', 'string'),
                Field('persona', 'reference person', requires=IS_IN_DB(db, 'person.id', '%(name)s')),
                Field('personb', 'reference person', requires=IS_IN_DB(db, 'person.id', '%(name)s'))
                     )

db.thing.persona.widget = SQLFORM.widgets.autocomplete(request, db.person.name, id_field=db.person.id)
db.thing.personb.widget = SQLFORM.widgets.autocomplete(request, db.person.name, id_field=db.person.id)


# controller
def create():
    form = SQLFORM(db[request.args(0)])
    form.process(detect_record_change=True)
    if form.record_changed:
        pass
        # do something
    elif form.accepted:
        pass
        # do something else
    else:
        pass
        # do nothing
    return dict(form=form)

You need to pass : person or thing as argument 0

Richard

Richard Vézina

unread,
Feb 2, 2016, 4:20:59 PM2/2/16
to web2py-users
You can workaround that problem like this :

db.define_table('person',
                Field('name', unique=True, requires=IS_NOT_IN_DB(db,'person.name')),
                Field('country', 'string'),
                Field('age', 'integer'))

db.define_table('thing',
                Field('name', 'string'),
                Field('persona', 'reference person', requires=IS_IN_DB(db, 'person.id', '%(name)s')),
                Field('personb', 'reference person', requires=IS_IN_DB(db, 'person.id', '%(name)s'))
                     )

db.thing.persona.widget = SQLFORM.widgets.autocomplete(request, db.person.name, id_field=db.person.id, keyword='_autocomplete_thing_persona')
db.thing.personb.widget = SQLFORM.widgets.autocomplete(request, db.person.name, id_field=db.person.id, keyword='_autocomplete_thing_personb')

Which was maybe the intent of having keyword attribute... I don't know...

Richard

Pierre

unread,
Feb 3, 2016, 7:00:47 AM2/3/16
to web2py-users
works !!!

very nice feature and very magic indeed 


is it robust or is it still experimental ?

Richard Vézina

unread,
Feb 3, 2016, 10:09:58 AM2/3/16
to web2py-users
autocomplete is there since web2py exist or almost I guess...

It handy, but it slow and I prefer to use twitter typehead autocomplete, much more works but it much better...

Reply all
Reply to author
Forward
0 new messages