#9230: Iterating over checkboxes in CheckboxSelectMultiple should be possible
----------------------------------+-----------------------------------------
Reporter: mtredinnick | Owner: mtredinnick
Status: new | Milestone:
Component: Forms | Version: 1.0
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by aeby):
* has_patch: 0 => 1
* needs_tests: 0 => 1
Comment:
I wrote a small patch to solve this problem. Needless to say that we have
to update all the 'choice' widgets the provide a solid solution.[[BR]]
This is a minimal solution intended as a basis for discussion.[[BR]]
An iteratable widget could look like this:
{{{
class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
def get_iterator(self, name, value, attrs):
"""
Alternatively we could pre-render here all the choices and store
them in a list.
"""
self.item = 0
self.name = name
self.attrs = attrs
if value is None: value = []
self.value = value
self.str_values = set([force_unicode(v) for v in value])
self.has_id = attrs and 'id' in attrs
self.final_attrs = self.build_attrs(attrs, name=name)
return self
def next(self):
if self.item >= len(self.choices):
raise StopIteration
if self.has_id:
final_attrs = dict(self.final_attrs, id='%s_%s' %
(self.attrs['id'], self.item))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
(option_value, option_label) = self.choices[self.item]
cb = CheckboxInput(final_attrs, check_test=lambda value: value in
self.str_values)
option_value = force_unicode(option_value)
rendered_cb = cb.render(self.name, option_value)
option_label = conditional_escape(force_unicode(option_label))
self.item += 1
return mark_safe(u'<label%s>%s %s</label>' % (label_for,
rendered_cb, option_label))
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/9230#comment:8>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---