Hmm...
I would first bring this piece of code into the controller as helpers which
are slow are suppose to be even slower if use from the view. It will make
the code less heavy...
I am not sure what you are exactly want. Since you already loop over you
myfields dict, you can just use the values you need from it, but you need
other level of iteration and split the construction of the SELECT and
OPTGROUP and OPTION to avoid too much iteration
I don't think optgroup are need to be dynamic as the are invariate, in your
example... If they are you will need a dict to create the
optgroup_tja_options lits I create below as you need to create dynamically
variable name and dict will prevent you poluting your name space and
managing variable.
Here a starting point :
optgroup_tja_options = []
optgroup_abc_options = []
for k, row in myfields.itertiems():
for opt_k, optgroup_options in row.iteritmes():
if opt_k == 'tja':
for opt in optgroup_options:
optgroup_tja_options.append(OPTION(opt['value'],
_value=opt['id']))
# repeat for the other OPTGROUP
Then:
SELECT(OPTGROUP(*optgroup_tja_options, _label="tja"), ...)
I think you outsource work Antonio!! :-P
I am not sure my code work, but you get an idea in which direction you need
to go...
But seriously, I think your data structure "myfields" sucks a bit, not sure
it what you get out from a web2py record, if it the case, I know why I
never use Option group in select ;-P
Good luck...
Richard
On Fri, Apr 28, 2017 at 7:36 AM, António Ramos <[email protected]> wrote:
> How do i change my code to dinamically create the OPTGROUP and OPTION from
> myfields
>
> {{myfields={
> "driver":{"tja":[{"value":"
> joe","id":1},{"value":"anne","id":2}],"abc":[{"value":"cris"
> ,"id":3},{"value":"pat","id":4}]},
> "truck":{"tja":[{"value":"
> j111","id":10},{"value":"j222","id":20}],"abc":[{"value":"
> j333","id":30},{"value":"j444","id":40}]},
> "galera":{"tja":[{"value":"g111","id":100},{"value":"
> g222","id":200}],"abc":[{"value":"g444","id":300},{"
> value":"g555","id":400}]},
>
> }
> }}
> {{for x in myfields:}}
> {{=LABEL(x,
> SELECT(
> OPTGROUP(
> OPTION('joe',_value="1"),
> OPTION('anne',_value="2"),
> _label="tja"),
> OPTGROUP(
> OPTION('cris',_value="3"),
> OPTION('pat',_value="4"),
> _label="abc"),
> _class="js-example-basic-single js-states
> form-control select2-hidden-accessible",_id=x,_tabindex="-1"
> ),_for=x)}}
>
> {{pass}}
>
> Thank you
>
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Sem
> vírus. www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#m_-568767702987022803_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> --
> 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.
>
--
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.