5π
β
1) So you can simply override yesno
which default value is ββ,ββ (it is just str
):
some_name = BooleanColumn(yesno='1,2')
or remove text:
some_name = BooleanColumn(yesno=',')
2) Using css
you can specify custom images (donβt forget set yesno=','
):
span.true {
background: url(../img/true.gif) top center no-repeat;
}
span.false {
background: url(../img/false.gif) top center no-repeat;
}
3) Specify some extra attrs to span
(but donβt specify class
!):
some_name = BooleanColumn(attrs={'span': {'style': 'color:blue'}})
4) If for some reasons you want change default class setting behaviour (true
or false
) β you should override BooleanColumn
and itβs method render
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django_tables2.utils import AttributeDict
class CustomBooleanColumn(BooleanColumn):
def render(self, value):
value = bool(value)
text = self.yesno[int(not value)]
html = '<span %s>%s</span>'
class_name = 'some_class_false'
if value:
class_name = 'some_class_true'
attrs = {'class': 'class_name'}
attrs.update(self.attrs.get('span', {}))
return mark_safe(html % (AttributeDict(attrs).as_html(), escape(text)))
And override your field
some_name = CustomBooleanColumn(yesno=',')
π€madzohan
0π
Here is full code now, thanks to madzohanβs answer. Note that I used django-bootstrap3 so that i can use the bootstrap icons:
from django_tables2 import BooleanColumn, Column, Table
from django.utils.safestring import mark_safe
from django_tables2.utils import AttributeDict
from manager.models import Partner
class BootstrapBooleanColumn(BooleanColumn):
def __init__(self, null=False, **kwargs):
if null:
kwargs["empty_values"] = ()
super(BooleanColumn, self).__init__(**kwargs)
def render(self, value):
value = bool(value)
html = "<span %s></span>"
class_name = "glyphicon glyphicon-remove"
if value:
class_name = "glyphicon glyphicon-ok"
attrs = {'class': class_name}
attrs.update(self.attrs.get('span', {}))
return mark_safe(html % (AttributeDict(attrs).as_html()))
class PartnerTable(Table):
country_name = Column(accessor='country.name', verbose_name='Country')
region_name = Column(accessor='country.region.name', verbose_name='Region')
aacsb = BootstrapBooleanColumn()
amba = BootstrapBooleanColumn()
equis = BootstrapBooleanColumn()
mba = BootstrapBooleanColumn()
bsc = BootstrapBooleanColumn()
msc = BootstrapBooleanColumn()
doubledegree = BootstrapBooleanColumn()
class Meta:
model = Partner
fields = ('name',
'country_name',
'region_name',
'website',
'aacsb',
'amba',
'equis',
'mba',
'bsc',
'msc',
'doubledegree',
)
π€Mike Rochov
Source:stackexchange.com