【问题标题】:Dynamically change the color of a cell in django在 django 中动态更改单元格的颜色
【发布时间】:2014-10-15 09:19:32
【问题描述】:

我想根据值动态更改单元格的背景颜色。 我使用 django-tables2 渲染我的表格并且工作正常。 这是我在模板中呈现表格的代码:

{% load querystring from django_tables2 %}
{% load trans blocktrans from i18n %}
{% load bootstrap_toolkit %}

{% if table.page %}
    <div class="table-container">
{% endif %}

{% block table %}
    <table class="table table-compact table-bordered"{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
        {% block table.thead %}
            <thead>
            <tr>
                {% for column in table.columns %}
                    {% if column.orderable %}
                        <th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th>
                    {% else %}
                        <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
                    {% endif %}
                {% endfor %}
            </tr>
            </thead>
        {% endblock table.thead %}
        {% block table.tbody %}
            <tbody>
            {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
                {% block table.tbody.row %}
                    <tr class="{% cycle "odd" "even" %}">
                        {% for column, cell in row.items %}
                            <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
                        {% endfor %}
                    </tr>
                {% endblock table.tbody.row %}
            {% empty %}
                {% if table.empty_text %}
                    {% block table.tbody.empty_text %}
                        <tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
                    {% endblock table.tbody.empty_text %}
                {% endif %}
            {% endfor %}
            </tbody>
        {% endblock table.tbody %}
        {% block table.tfoot %}
            <tfoot></tfoot>
        {% endblock table.tfoot %}
    </table>
{% endblock table %}
   </div>
{% if table.page %}
    {% block pagination %}
        {{ table.page|pagination }}
    {% endblock pagination %}
{% endif %}

我怀疑 django-tables2 或 Django 是否有一些属性来评估单元格的值,或者我需要创建一个 JavaScript 函数。 请给我一些链接或sn-p来实现这个功能

提前致谢

【问题讨论】:

  • 您寻找的价值是否会经常变化,或者您寻找的价值是否相同? {% if %} 会起作用吗?
  • 值不断变化,我尝试使用 {% if %} 但收到错误:无法解析剩余部分 '% If cell.e_reception == 2 %'。我会搜索这个错误。如果我找到什么我会让你知道的。感谢回复

标签: javascript ajax django html-table django-tables2


【解决方案1】:

这个答案已经很久了,但您现在可以在您的专栏中使用attrs 来执行此操作,如here 所示。

class StatusColumn(Column):
    attrs = {'td': {'class': lambda value: f'status-{value}'}}

这将设置&lt;td /&gt; 元素上的class HTML 属性以匹配单元格的值。

此外,现在{% render_table ... %} 所做的很多工作似乎都是您手动完成的,因此,如果您重新审视这一点,请看看您可以使用新功能简化哪些工作。

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 2012-06-17
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多