如何获取排除的字段值以更改行背景

时间:2018-10-21 11:22:33

标签: django django-tables2

我想显示一个排除了某些字段的表,因为我不想在表中显示它们。 同时,我想根据排除的值更改行中的颜色。可以使用django-table2做到这一点吗?

import django_tables2 as tables
from web_logic.models import Stations
from django_tables2.utils import A


class StationTable(tables.Table):
    """

    """

    station_id = tables.LinkColumn()
    rack_sum = tables.LinkColumn("racks_page", args=[A('pk')], verbose_name='Кол-во стоек')
    status = tables.BooleanColumn()

    def __init__(self, *args, **kwargs):
        super(StationTable, self).__init__(*args, **kwargs)

    def render_station_id(self, value):
        print(value)
        return "%s " % value

    class Meta:
        model = Stations
        exclude = (
            "id",
            "onboot_time",
            'certificat_key',
            'private_key',
            "status",
            "ipadress",
            'latitude',
            'longitude',
        )
        template_name = 'django_tables2/bootstrap.html'
        attrs = {
            "class": "table",
            "td": {"class": "table-hover",},
            "thead": {"class": "table-dark"},
        }

例如,状态字段描述整个行的状态。它可以是对或错。我不想显示带有该字段的列,但是我想基于该字段的值突出显示(更改线条的颜色)。我该怎么办?

1 个答案:

答案 0 :(得分:0)

是的,您可以使用row_attrs进行此操作:

class StationTable(tables.Table):
    station_id = tables.LinkColumn()
    rack_sum = tables.LinkColumn("racks_page", args=[A('pk')], verbose_name='Кол-во стоек')

    def render_station_id(self, value):
        return "%s " % value

    class Meta:
        model = Stations
        exclude = (
            "id",
            "onboot_time",
            'certificat_key',
            'private_key',
            "status",
            "ipadress",
            'latitude',
            'longitude',
        )
        template_name = 'django_tables2/bootstrap.html'
        attrs = {
            "class": "table",
            "td": {"class": "table-hover",},
            "thead": {"class": "table-dark"},
        }
        row_attrs = {
            "class": lambda record: "info" if record.status else ""
        }

这会将属性class="info"添加到具有<tr>的行的status = True标签中。

相关问题