Javascript 动态更改django中单元格的颜色
我想根据值动态更改单元格的背景色。 我使用django-tables2呈现我的表,效果很好。 这是我在模板中呈现表的代码:Javascript 动态更改django中单元格的颜色,javascript,ajax,django,html-table,django-tables2,Javascript,Ajax,Django,Html Table,Django Tables2,我想根据值动态更改单元格的背景色。 我使用django-tables2呈现我的表,效果很好。 这是我在模板中呈现表的代码: {% load querystring from django_tables2 %} {% load trans blocktrans from i18n %} {% load bootstrap_toolkit %} {% if table.page %} <div class="table-container"> {% endif %} {% b
{% 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 %}
{%load querystring from django_tables2%}
{%load trans blocktrans from i18n%}
{%load bootstrap_toolkit%}
{%if table.page%}
{%endif%}
{%block table%}
{%block table.thead%}
{table.columns%}
{%if column.orderable%}
{%else%}
{{column.header}}
{%endif%}
{%endfor%}
{%endblock table.thead%}
{%block table.tbody%}
{table.page.object_list中的行的%{默认值:table.rows%}{{支持分页}
{%block table.tbody.row%}
{列的百分比,行中的单元格。项%}
{{cell}}
{%endfor%}
{%endblock table.tbody.row%}
{%empty%}
{%if table.empty_text%}
{%block table.tbody.empty_text%}
{{table.empty_text}
{%endblock table.tbody.empty_text%}
{%endif%}
{%endfor%}
{%endblock table.tbody%}
{%block table.tfoot%}
{%endblock table.tfoot%}
{%endblock表%}
{%if table.page%}
{%块分页%}
{{table.page}分页}
{%endblock分页%}
{%endif%}
我怀疑django-tables2或django是否有一些属性来计算单元格的值,或者我需要创建一个JavaScript函数。
请给我一些链接或代码片段来实现这个功能
提前感谢这个答案需要很长时间才能得到,但现在您可以在专栏中使用
attrs
来实现这一点,如图所示
这将在
元素上设置class
HTML属性以匹配单元格的值
此外,看起来您正在手动执行许多
{%render\u table…%}
现在所做的工作,因此如果您再次访问此功能,一定要看看您可以通过更新的功能简化哪些内容。您要找的值是否会经常更改,或者您要使不同的值是否相同?{%if%}是否有效?值不断变化,我尝试使用{%if%},但收到一个错误:无法分析剩余值“%if cell.e_receivement==2%”。我将搜索此错误。如果我发现了什么,我会告诉你的。谢谢你的回复
class StatusColumn(Column):
attrs = {'td': {'class': lambda value: f'status-{value}'}}