django-tables2 - 根据特定列的值更改整行的背景颜色

django-table2 - Change background color for entire row depending on the value of specific column

我一直在尝试使用 django-table2 包突出显示 table 的整行。

我通过以下操作成功更改了一条记录的字体颜色:

def render_MyValue(self, value, column, record):
   if record['Warning']:
       column.attrs = {'td': {'style': 'color:darkorange;'}}
   else:
       column.attrs = {'td': {'style': 'color:black;'}}
   return value

在下面找到我的 table class:

class DetailedReportTable(tables.Table):
    MyValue = tables.Column(orderable=False, verbose_name='Value')
    ...
    Warning = tables.Column(orderable=False, visible=False)

问题是如果警告为真,我找不到如何将行的背景设置为橙色。

按照文档 here 我还尝试了以下内容:

class DetailedReportTable(tables.Table):
    ...

    class Meta:
        row_attrs = { "bg-color": lambda record: "#8B0000" if record['Warning'] else "#000000" }

但这什么也没做...

如何使用 django-table2 更改一行的背景颜色?

您尝试的方法很接近,但您只是在行 html 元素上设置 "bg-color" 的属性 - 该属性不存在。相反,您想要设置 class,您可以在 CSS 中设置样式,或者直接设置样式属性。这是第二个选项:

class DetailedReportTable(tables.Table):
    ...

    class Meta:
        row_attrs = { "style": lambda record: "background-color: #8B0000;" if record['Warning'] else "background-color: #000000;" }