コード例 #1
0
    def render_options(self, choices, selected_choices, disabled_choices):
        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))
        # Normalize to strings.
        selected_choices = set([force_unicode(v) for v in selected_choices])
        disabled_choices = set([force_unicode(v) for v in disabled_choices])
        output = []
        for option_value, option_label in chain(self.choices, choices):
            if isinstance(option_label, (list, tuple)):
                output.append('<optgroup label="%s">' % escape(force_unicode(option_value)))
                for option in option_label:
                    output.append(render_option(*option))
                output.append('</optgroup>')
            else:
                output.append(render_option(option_value, option_label))
        return '\n'.join(output)


##====================================================================================##
コード例 #2
0
 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))