Put the empty option on top in DropDownMultiple.
authorMatthijs Kooijman <matthijs@stdin.nl>
Tue, 3 Feb 2009 22:00:14 +0000 (23:00 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Tue, 3 Feb 2009 22:00:14 +0000 (23:00 +0100)
Previous changes that started reusing Select.render_options had the side
effect of moving the empty option to the bottom. This restores the
original behaviour.

tools/widgets/dropdownmultiple.py

index 5bbe27ef5691c00deff4c674707aad24624e1d9a..10c7a2b98883c86eabf9a1eb6acf965b8c378343 100644 (file)
@@ -75,8 +75,12 @@ class DropDownMultiple(widgets.SelectMultiple):
         id = final_attrs['id']
         del final_attrs['id']
 
-        # Insert blank value
-        choices = [('','---')] + list(choices)
+        # Insert blank value. We insert this in self.choices, because
+        # render_options merges self.choices with its choices argument
+        # (in that order) and we want to have the empty option at the
+        # top.
+        old_choices = self.choices
+        self.choices = [('','---')] + list(self.choices)
 
         # Build values
         items = []
@@ -91,6 +95,9 @@ class DropDownMultiple(widgets.SelectMultiple):
 
         script = TPL_SCRIPT %{'id': id}
         output = TPL_FULL %{'id': id, 'values': '\n'.join(items), 'script': script}
+        
+        # Restore the original choices
+        self.choices = old_choices
 
         return mark_safe(output)