【问题标题】:How to get the field value that is excluded in order to change the row background如何获取排除的字段值以更改行背景
【发布时间】:2019-03-25 16:02:15
【问题描述】:

我想显示一个排除了某些字段的表格,因为我不想在表格中显示它们。 同时,我想根据排除的值更改行中的颜色。 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"},
        }

例如,状态字段描述了整行的状态。它可以是真或假。我不想显示带有此字段的列,但我想根据此字段的值突出显示(更改线条的颜色)。我该怎么做?

【问题讨论】:

    标签: django django-tables2


    【解决方案1】:

    是的,您可以使用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" 添加到具有status = True 的行的<tr>-tag 中。

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2016-03-15
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2011-04-08
      • 1970-01-01
      • 2021-02-23
      相关资源
      最近更新 更多