Mike Orr avatar Mike Orr committed 5277c09

Reverse order of select options, now (value, label).

Comments (0)

Files changed (2)

tests/test_modeltags.py

         )
     def test_select(self):
         self.assertEqual(
-            self.m.select("lang", [("English", "en"), ("German", "de"), ("Japanese", "jp")]),
+            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
             u'<select name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
         )
 
         )
     def test_select(self):
         self.assertEqual(
-            self.m.select("lang", [("English", "en"), ("German", "de"), ("Japanese", "jp")]),
+            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
             u'<select name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
         )
 
         )
     def test_select(self):
         self.assertEqual(
-            self.m.select("lang", [("English", "en"), ("German", "de"), ("Japanese", "jp")]),
+            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
             u'<select id="person:lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
         )
 
         )
     def test_select(self):
         self.assertEqual(
-            self.m.select("lang", [("English", "en"), ("German", "de"), ("Japanese", "jp")]),
+            self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
             u'<select name="lang">\n<option value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
         )        
 if __name__ == '__main__':

webhelpers/html/tags.py

     * ``selected_values`` -- a string or list of strings or integers giving
       the value(s) that should be preselected.
 
-    * ``options`` -- an iterable of ``(label, value)`` pairs.  The label
+    * ``options`` -- an iterable of ``(value, label)`` pairs.  The label
       is what's shown to the user; the value is what's seen by the 
-      application if the user chooses this option.  Hint: you can use
-      ``sorted(options)`` to display the labels alphabetically.  You can
-      also pass an iterable of strings, in which case the labels and values
-      will be identical.
+      application if the user chooses this option.  You can also pass an
+      iterable of strings, in which case the labels and values will be
+      identical.  
+
+      CAUTION: the label used to be first in the pair in the old rails helper
+      ``options_for_select`` had the label first in the pair, and in earlier
+      development versions of this helper.  The order was changed because
+      most real-life lists have the value first, including ``dict.items()``.
+
+      HINT: You can sort options alphabetically by label via:
+      ``sorted(my_options, key=lambda x: x[1])``
 
     * ``multiselect`` -- if true, this control will allow multiple
        selections.
 
     Examples (call, result)::
     
-        >>> select("currency", "$", [["Dollar", "$"], ["Kroner", "DKK"]])
+        >>> select("currency", "$", [["$", "Dollar"], ["DKK", "Kroner"]])
         literal(u'<select name="currency">\\n<option selected="selected" value="$">Dollar</option>\\n<option value="DKK">Kroner</option>\\n</select>')
         >>> select("cc", "MasterCard", [ "VISA", "MasterCard" ], id="cc", class_="blue")
         literal(u'<select class="blue" id="cc" name="cc">\\n<option value="VISA">VISA</option>\\n<option selected="selected" value="MasterCard">MasterCard</option>\\n</select>')
     selected_values = map(unicode, selected_values)
     for option in options:
         if isinstance(option, basestring):
-            label = value = option
-        elif isinstance(option, int):
-            label = value = unicode(option)
+            value = label = option
+        elif isinstance(option, (int, long)):
+            value = label = unicode(option)
         else:
-            if isinstance(option[0], literal):
-                label = option[0]
-            else:
-                label = unicode(option[0])
-            value = unicode(option[1])
+            value, label = option[:2]
+            value = unicode(value)
+            if not isinstance(label, literal):
+                label = unicode(label)
         if value in selected_values:
             opt = HTML.option(label, value=value, selected="selected")
         else:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.