Switch select box parameter using radio button.

38 views
Skip to first unread message

Florent Barra

unread,
Oct 3, 2013, 7:46:35 AM10/3/13
to nagare...@googlegroups.com
Hello, 

I'm trying to display a select box depending on the option chosen (2 options with 2 select box, if we select option 1 we display the first select box).
I'm trying to do this choice using radio button or html link but in both case, it doesn't work.
here my code :

#---------------------------------------------------------------------------------------------------------------------------
from __future__ import with_statement
from nagare import editor, presentation, component, validator

class Option:
def __init__(self):
self.option2 = 'opt1'
self.option_result = ''
class OptionEditor(editor.Editor):
def __init__(self, option):
self.list1 = ['a', 'b', 'c']
self.list2 = ['1', '2', '3']
self.option = 'opt1'
super(OptionEditor, self).__init__(option, ('option_result', 'option2'))
def test(self, opt):
self.option = opt
def commit(self):
super(OptionEditor, self).commit(('option_result'))
@presentation.render_for(OptionEditor)
def render(self, h, comp, *args):
with h.form:
h << 'option1 '  << h.input(type='radio', name='option').action(self.test('opt1')).selected(self.option == 'opt1') << h.br #<< h.a('X').action(self.test('opt1')) << h.br
h << 'option2 '  << h.input(type='radio', name='option').action(self.test('opt2')).selected(self.option == 'opt2') << h.br #<< h.a('X').action(self.test('opt2')) << h.br

with h.select(multiple='multiple').action(self.option_result):
if self.option == 'opt1':
with h.optgroup(label='Option 1'):
for i in self.list1 :
h << h.option(i, value=i).selected(self.option_result())
else :
with h.optgroup(label='Option 2'):
for i in self.list2 :
h << h.option(i, value=i).selected(self.option_result())
return h.root

class App:
    def __init__(self):
        self.option = Option()
        editor = OptionEditor(self.option)
        self.editor_component = component.Component(editor)

@presentation.render_for(App)
def render(self, h, *args):
h << self.editor_component
return h.root

app = App
#---------------------------------------------------------------------------------------------------------------------------

Despite the choice made, 'option' is always equal to 'opt2', and i don't understand why.

apoirier

unread,
Oct 3, 2013, 8:11:51 AM10/3/13
to nagare...@googlegroups.com
Hi Florent,
Here you're always  calling `self.test('op1')` and then `self.test('opt2')` when the view is rendered. That's why `option` is set to 'opt2'. What you want instead is to register callbacks because you want `self.test('op1')` or `self.test('op2')` to be called later, when the user will submit the form. So, just change your actions registration code by `...action(lambda: self.test('op1'))` and `...action(lambda: self.test('opt2')`

Florent Barra

unread,
Oct 4, 2013, 1:33:41 AM10/4/13
to nagare...@googlegroups.com
thank you 
it work fine now.
Reply all
Reply to author
Forward
0 new messages