def render(self, name, value, attrs=None, choices=()): to_opts = self.rel.to._meta if attrs is None: attrs = {} attrs['class'] = 'select2ajax' if self.multiple: attrs['multiple'] = 'multiple' attrs['data-search-url'] = self.admin_view.get_admin_url( '%s_%s_changelist' % (to_opts.app_label, to_opts.module_name)) attrs['data-placeholder'] = _('Search %s') % to_opts.verbose_name attrs['data-choices'] = '?' if self.rel.limit_choices_to: for i in list(self.rel.limit_choices_to): attrs['data-choices'] += "&_p_%s=%s" % (i, self.rel.limit_choices_to[i]) attrs['data-choices'] = format_html(attrs['data-choices']) if value: attrs['data-label'] = self.label_for_value(value) if value is None: value = '' final_attrs = self.build_attrs(attrs, type='text', name=name) if value != '': # Only add the 'value' attribute if a value is non-empty. final_attrs['value'] = force_text(self._format_value(value)) return format_html('<input{0} />', dutils.flatatt(final_attrs))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] if attrs: attrs['class'] = attrs.get('class', '') + ' %s dropdown-menu open'%self.base_css else: attrs['class'] = '%s dropdown-menu open'%self.base_css final_attrs = self.build_attrs(attrs, name=name) label_list = [] output = [u'<div%s role="combobox">' % dutils.flatatt(final_attrs)] # Normalize to strings if self.base_css=='admin-m2m-tree': str_values = set([force_unicode(v) for v in value]) else: str_values = [force_unicode(value)] self.fill_output(output, self.choices, str_values, label_list) raw_str = '' if self.base_css=='admin-fk-tree': raw_str = '<input type="hidden" id="id_%s" name="%s" value="%s"></input>'%(name, name,str_values and str_values.pop() or '') output.append(u'</div>') wapper = ''' <div class="dropdown">%s <button type="button" class="btn dropdown-toggle btn-default bs-placeholder" data-toggle="dropdown" role="button" title="点击下拉选择" aria-expanded="true"><span class="filter-option pull-left">%s</span> <span class="bs-caret"><span class="caret"></span></span></button> '''%(raw_str, (', '.join(label_list) or '请选择...')) output.insert(0, wapper) output.append(u'</div>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None): if value is None: value = '' final_attrs = self.build_attrs(attrs, name=name) final_attrs['class'] = 'nav nav-pills nav-stacked' output = [u'<ul%s>' % dutils.flatatt(final_attrs)] options = self.render_options(force_unicode(value), final_attrs['id']) if options: output.append(options) output.append(u'</ul>') output.append('<input type="hidden" id="%s_input" name="%s" value="%s"/>' % (final_attrs['id'], name, force_unicode(value))) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None): if value is None: value = "" final_attrs = self.build_attrs(attrs, name=name) final_attrs["class"] = "nav nav-pills nav-stacked" output = [u"<ul%s>" % dutils.flatatt(final_attrs)] options = self.render_options(force_unicode(value), final_attrs["id"]) if options: output.append(options) output.append(u"</ul>") output.append( '<input type="hidden" id="%s_input" name="%s" value="%s"/>' % (final_attrs["id"], name, force_unicode(value)) ) return mark_safe(u"\n".join(output))
def render(self, name, value, attrs=None, choices=()): if attrs is None: attrs = {} attrs['class'] = '' if self.is_stacked: attrs['class'] += 'stacked' if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) selected_choices = set(force_unicode(v) for v in value) available_output = [] chosen_output = [] for option_value, option_label in chain(self.choices, choices): if isinstance(option_label, (list, tuple)): available_output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value))) for option in option_label: output, selected = self.render_opt(selected_choices, *option) if selected: chosen_output.append(output) else: available_output.append(output) available_output.append(u'</optgroup>') else: output, selected = self.render_opt(selected_choices, option_value, option_label) if selected: chosen_output.append(output) else: available_output.append(output) context = { 'verbose_name': self.verbose_name, 'attrs': attrs, 'field_id': attrs['id'], 'flatatts': dutils.flatatt(final_attrs), 'available_options': u'\n'.join(available_output), 'chosen_options': u'\n'.join(chosen_output), } return mark_safe( loader.render_to_string('xadmin/forms/transfer.html', context))
def render(self, name, value, attrs=None, choices=()): if attrs is None: attrs = {} attrs['class'] = '' if self.is_stacked: attrs['class'] += 'stacked' if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) selected_choices = set(force_unicode(v) for v in value) available_output = [] chosen_output = [] for option_value, option_label in chain(self.choices, choices): if isinstance(option_label, (list, tuple)): available_output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value))) for option in option_label: output, selected = self.render_opt( selected_choices, *option) if selected: chosen_output.append(output) else: available_output.append(output) available_output.append(u'</optgroup>') else: output, selected = self.render_opt( selected_choices, option_value, option_label) if selected: chosen_output.append(output) else: available_output.append(output) context = { 'verbose_name': self.verbose_name, 'attrs': attrs, 'field_id': attrs['id'], 'flatatts': dutils.flatatt(final_attrs), 'available_options': u'\n'.join(available_output), 'chosen_options': u'\n'.join(chosen_output), } return mark_safe(loader.render_to_string('xadmin/forms/transfer.html', context))
def render(self, name, value, attrs=None): if value is None: value = '' self.attrs.update({'class': '%s textinput' % self.attrs.get('class', '')}) final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) if value != '': final_attrs['value'] = force_text(self._format_value(value)) templ = """ <input{0} onkeyup='javascript:count(this);' /> <span class='label label-default'>已输入<span class='counter'>%s</span>个字</span> """ % len(value) script = """ <script> function count(field){ var counter_label = $(field).next('span').children('span'); counter_label.html($(field).val().length); } </script> """ html = format_html(templ, dutils.flatatt(final_attrs)) return '%s%s' % (html, script)