如何更改Django-tables行的颜色?

时间:2016-05-29 18:29:49

标签: django django-models html-table django-tables2

是否可以根据当前对象的值更改行的颜色?

就我而言,我从模型table创建了JobJob具有属性delivery。如果job.delivery例如'已交付',我想将行的颜色更改为红色。

我唯一想到的就是使用JQuery,但我不确定这不是一种矫枉过正的行为。

class MyOrdersTable(tables.Table):
    edit_entries = tables.TemplateColumn(
            '{% if not record.translator %}<a href="/jobs/update/{{record.id}}">Edit Order</a>{% else %} Can\'t edit order, translator has been assigned. {% endif %}')
    price = tables.Column(default='Not Yet',verbose_name='Price')
    translator = tables.Column(default='Not Yet',verbose_name='Translator')
    progress = tables.TemplateColumn('{{record.delivery.get_status_display}}',verbose_name='Progress')

    class Meta:
        model = Job
        attrs = {'class': 'table table-striped table-bordered table-hover', 'width': '70%'}
        fields = (
            'translator', 'short_description', 'language_from', 'language_to', 'level', 'created', 'modified', 'price',
            'progress','edit_entries')
        empty_text = """You haven't order a translation yet. Can we help you?"""

1 个答案:

答案 0 :(得分:3)

是的,从django-tables2 v1.2.0开始,您可以使用row attributes

class MyOrdersTable(tables.Table):
    # [...]
    class Meta:
        model = Jub
        row_attrs = {
            'data-delivery': lambda record: record.delivery
        }

这将呈现如下行:

<tr [...] data-delivery="delivered"><td>....</tr>
<tr [...] data-delivery="peding"><td>....</tr>

您可以使用data属性来定位具有一些CSS的行:

tr[data-delivery='deliverd'] {
    background-color: #f2dede;
}