Django设置类别< ul>而不是< input>与CheckboxSelectMultiple小部件 [英] Django Setting class of <ul> instead of <input> with CheckboxSelectMultiple widget
问题描述
我可以设置一个小部件的类,如
self.widget = forms.CheckboxSelectMultiple(attrs = 'class':'myclass'})
但这适用于 myclass
到所有
< li>
元素,例如
< UL>
< label>< li>< input type =checkboxclass =myclass> 1< / li>< / label>
< label>< li>< input type =checkboxclass =myclass> 2< / li>< / label>
< label>< li>< input type =checkboxclass =myclass> 3< / li>< / label>
< / ul>
如何将类应用于< ul>
元素?
默认情况下,您无法设置UL标签上的属性。 容易的事情是将CheckboxSelectMultiple子类化以进行所需的操作。两种方法是:
使用ulattrs参数构建CheckboxSelectMultiple的自定义版本。
class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
def render(self,name,value,ulattrs = None,attrs = None,choices =()):
如果值为None:value = []
has_id = attrs和'id'in attrs
final_attrs = self.build_attrs(attrs,name = name)
output = [u'
#规范化为字符串
str_values = set([force_unicode(v)for v in value])
for i,(option_value,在$枚举(chain(self.choices,choices))中,
#如果给出了一个ID属性,则添加一个数字索引作为后缀
#,以便复选框不全都具有相同的ID属性。
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:str_values中的值)
option_value = force_unicode(option_value)
rendered_cb = cb.render(name,option_value)
option_label = conditional_escape(force_unicode(option_label))
output.append(u'< li>< label %s>%s%s< / label>< / li>'%(label_for,rendered_cb,option_label)
output.append(u'< / ul>')
return mark_safe你可以做一些更多的事情,你可以做一些更多的事情hacky ... class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
def render(self,name,值,attrs = None,choices =()):
html = super(MyCheckboxSelectMultiple,self).render(name,value,attrs,choices)
return mark_safe(html.replace '< ul>','< ul class =foobar>'))
I can set the class of a widget with a line like
self.widget = forms.CheckboxSelectMultiple(attrs={'class': 'myclass'})
but this applies myclass
to all of the <li>
elements, e.g.,
<ul>
<label><li><input type="checkbox" class="myclass">1</li></label>
<label><li><input type="checkbox" class="myclass">2</li></label>
<label><li><input type="checkbox" class="myclass">3</li></label>
</ul>
How can I apply a class to only the <ul>
element?
解决方案 By default you cannot set the attribute on the UL tag. The "easy" thing to do is subclass the CheckboxSelectMultiple to do what you want. Two approaches are:
Construct a custom version of the CheckboxSelectMultiple with a "ulattrs" argument.
class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
def render(self, name, value, ulattrs=None, 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'<ul class="%s">' % ulattrs.get('class')]
# Normalize to strings
str_values = set([force_unicode(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))
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)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_unicode(option_label))
output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
output.append(u'</ul>')
return mark_safe(u'\n'.join(output))
or, you could do something a little more hacky...
class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
def render(self, name, value, attrs=None, choices=()):
html = super(MyCheckboxSelectMultiple, self).render(name, value, attrs, choices)
return mark_safe(html.replace('<ul>', '<ul class="foobar">'))
这篇关于Django设置类别< ul>而不是< input>与CheckboxSelectMultiple小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!