def render_option(option_value, option_label): option_value = force_unicode(option_value) ## This next line adds a mesasge after the label for the option. Modify as needed ## option_label = (option_value in disabled_choices) and (force_unicode(option_label) + ' - SOLD OUT') or force_unicode(option_label) selected_html = (option_value in selected_choices) and ' selected="selected"' or '' disabled_html = (option_value in disabled_choices) and ' disabled="disabled"' or '' return '<option value="%s"%s%s>%s</option>' % ( escape(option_value), selected_html, disabled_html, conditional_escape(option_label))
def render(self, name=None, value=None, attrs=None, choices=()): name = name or self.name value = value or self.value attrs = attrs or self.attrs if "id" in self.attrs: label_for = ' for="%s_%s"' % (self.attrs["id"], self.index) else: label_for = "" choice_label = conditional_escape(force_unicode(self.choice_label)) return mark_safe(u"%s<label%s>%s</label>" % (self.tag(), label_for, choice_label))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<div class="landmarks-listing"><ul class="unstyled">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label, option_detail) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) option_detail = force_unicode(option_detail) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label><div>%s</div></li>' % (label_for, rendered_cb, option_label, option_detail)) output.append(u'</ul></div><div id="landmark-details"></div><div class="clear"></div>') return mark_safe(u'\n'.join(output))
def __unicode__(self): choice_label = conditional_escape(force_text(self.choice_label)) return mark_safe(u'%s %s' % (self.tag(), choice_label))
def render(self, name, value, attrs=None, choices=()): ''' This render function is copied from django/forms/widgets.py... ...and altered several times. ''' attrs = attrs or {} if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i), style='display:inline;') label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=(lambda value: value in str_values)) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_text(option_label)) output.append(u'<label%s style="display:block;">%s %s</label>' % (label_for, rendered_cb, option_label)) # Optionally wrap it in spans COLUMNS, MINVALUES = 3, 8 if len(output) > MINVALUES: per_column = int(float(len(output) + COLUMNS - 1) / COLUMNS) for i in reversed(range(per_column, len(output), per_column)): output.insert(i, ('</span><span class="column"' ' style="display:block;">')) output.insert(0, ('<span class="large-multiple-choice">' '<span class="column" style="display:block;">')) output.append('</span><br style="clear:both;"/></span>') else: # Always add the large-multiple-choice classes.. otherwise the # layout will mismatch. output.insert(0, ('<span class="large-multiple-choice">' '<span class="column" style="display:block;">')) output.append('</span><br style="clear:both;"/></span>') # Code below is from old checkboxselectmultiplewithjs buttons = ' / '.join( ('''<a href="#" onclick="var checkboxes=document.''' '''getElementsByName('%s');''' '''for(var i=0;i<checkboxes.length;i++){''' '''checkboxes[i].checked=%s;}return false;">%s</a>''') % (i[0], i[1], i[2]) for i in ((name, 'true', 'Select All'), (name, 'false', 'None'))) output = ('<span class="large-multiple-choice-quickselect">' + buttons + '</span><br/>' + u'\n'.join(output)) return mark_safe(output)