The problem is that you are passing the same OPTION objects to SELECT each
time, and when you set the "value" attribute of SELECT, it *mutates* the
OPTION objects. So, the final pass through the loop sets the OPTION
objects, which appear in each of the SELECT objects.
Instead, you should create new OPTION objects for each SELECT.
Also, there is no reason to create a "shifts" dictionary -- just iterate
over a list:
for row in ['1', '2', '3']:
Also, keep in mind that unless you are using Python 3.7, there is no
guarantee regarding the order of iteration over dictionary keys.
Anthony
On Wednesday, June 6, 2018 at 10:01:47 AM UTC-4, Ischa Guns wrote:
>
> Hello,
>
> I am trying to make a custom form with select boxes. I am using the SELECT
> helper to create the select boxes. I have a problem with setting the
> selected value. I have created the following example.
>
> def test_select():
> users = [('0', 'None'),
> ('1', 'John'),
> ('3', 'Someone Else'),
> ('2', 'Jane')
> ]
> resources = [OPTION(x[1], _value=x[0]) for x in users]
> shifts = {1:'1',2:'2',3:'3'}
> tab = TABLE()
> trow = TR()
> for row in shifts:
> trow.append(TD(SELECT(*resources,
> _name='name',
> _id='name',
> value=shifts[row],
> _style="background-color:%s;" % 'yellow')
> )
> )
> tab.append(trow)
> form = FORM(tab)
> return dict(form=form)
>
> I am trying to set the value by looping through the "shifts" dictionary
> and assigning the "value" to the value from the dictionary. I expected that
> the first dropdown would be set to "John", the second to "Jane" and the 3rd
> to "Someone Else", but this is not happening. All 3 the dropdown lists are
> set to "Someone Else".
>
> How can I assign the value of the select box properly?
>
--
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.