Rendering multiple select dropdowns with a list of values #43

Closed
krowbar opened this Issue Jan 28, 2011 · 5 comments

Projects

None yet

3 participants

@krowbar

When (re) rendering Dropdowns with multiple selects, one can only pass a single value:

fdSelect = form.Form(
     form.Dropdown('Fire',
         ['q', 'b', 'c'],
         form.notnull,
         **{'multiple': None, 'size': 2}
         ),
 )

fd.Fire.value = 'b'
fd.render() works as expected:
...\n  b\n  c\n....
fd.Fire.value = ['a', 'b', 'c']
fd.render() does not select anything
...\n  b\n  c\n....

This patch seems to fix that behaviour:

--- form.py.orig    2011-01-28 11:24:58.080150680 -0600
+++ form.py 2011-01-28 11:28:59.346763259 -0600
@@ -240,8 +240,10 @@
             else:
                 value, desc = arg, arg 
 
-            if self.value == value: select_p = ' selected="selected"'
-            else: select_p = ''
+            if (self.value is not None and value in self.value):
+                select_p = ' selected="selected"'
+            else:   
+                select_p = ''
             x += '  %s\n' % (select_p, net.websafe(value), net.websafe(desc))
             
         x += '\n'

@krowbar

Oops...forgot the markdown


--- form.py.orig 2011-01-28 11:24:58.080150680 -0600
+++ form.py 2011-01-28 11:28:59.346763259 -0600
@@ -240,8 +240,10 @@
else:
value, desc = arg, arg

  • if self.value == value: select_p = ' selected="selected"'
  • else: select_p = ''
  • if (self.value is not None and value in self.value):
  • select_p = ' selected="selected"'
  • else:
  •            select_p = ''
         x += '  <option%s value="%s">%s</option>\n' % (select_p, net.websafe(value), net.websafe(desc))
    
     x += '</select>\n'
    

@aaronsw

This doesn't seem like it would work if the form wasn't a multiple...shouldn't it be something like:

if (self.value == value or (self.value and value in self.value)
@aaronsw

Also, it's easier for us to take patches if they're in the form of forks.

@krowbar

https://github.com/krowbar/webpy/tree/master/web
has a fork with your suggested syntax.
Your correct, my original version did not work in the case you suggested.

@anandology

Fixed rendering of Dropdown with mutliple values. (closed by 8beef76)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment