Hi,

not sure, what kind of form you're using. Expecting you use SQLFORM, I 
solved this in similar case as follows:
form[0].insert(-1, TR(TD(LABEL(T('City')),
                      TD(SELECT(_name='city', *[OPTION('{} 
{}'.format(city.code, city.name), _value=city.id)
                         for city in db(db.city.id>0).select(db.city.ALL, 
orderby=city.code)], value=this_city.id)))))

this_city a preselected value and the one matching the record currently 
processed.

Since on reload (after submit) the db is requested again, the select list 
should be updated.

For further information on extra fields in an SQLFORM --> 
http://web2py.com/books/default/chapter/29/07/forms-and-validators#Adding-extra-form-elements-to-SQLFORM

Have a try, hope it helps!

Best regards
Clemens


On Tuesday, August 18, 2020 at 2:32:57 PM UTC+2 roge...@gmail.com wrote:

> Hi
>
> I want a drop down list that shows all the values from existing records 
> AND allows the user to add a new value if they want.
>
> I have got something working using https://select2.org/ . But my approach 
> is questionable.
>
> The problem i have is that when a new value is added and the form is 
> submitted, the widget code is called *before* the new value is persisted to 
> the database. So when the form reloads after successfully being processed, 
> the dropdown doesn't have the new item and so can't be displayed.
>
> If i then reload the page the new item is included and all is well.
>
> I got around this by delaying the creation of the Select2 dropdown. 
> Instead of the widget immediately building the Select2 it executes an AJAX 
> call which grabs the dropdown data and then builds it. Of course this is 
> done after the form has been accepted so it has the new entry and all works 
> well.
>
> Is this the best way to do it?
>
> Or can i somehow set the widget **after** the form has been created?
>
> Thanks
> Andrew
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/20a6a0d5-dec6-4fcc-a6d1-30421a7d2eadn%40googlegroups.com.

Reply via email to