html - html表中行的条件样式
问题描述
我有一个 html 表,它从 PY 中的 SQL 查询中获取数据。我的 SQL 表中的一列是类型 (tp)。我希望 html 表为相同类型的每一行显示一种颜色,而另一种颜色显示相反的颜色。这是一本财务日记,因此当您输入费用 SQL 时会保存该信息。我希望表格以红色显示历史记录和费用行,以绿色显示收入行。这是我的 PY 和 HTML 代码。请帮助我迷路了
@app.route("/history")
@login_required
def history():
"""Show history of inputs"""
inputs = db.execute("""
SELECT amount, category, tp, transacted
FROM inputs
WHERE user_id = :user_id
ORDER BY transacted ASC
""", user_id=session["user_id"])
return render_template("history.html", inputs=inputs)
{% extends "layout.html" %}
{% block title %}
History
{% endblock %}
{% block main %}
<table class="table table-striped">
<thead>
<tr>
<th>Type</th>
<th>Amount</th>
<th>Category</th>
<th>Transacted</th>
</tr>
</thead>
<tbody>
{% for input in inputs %}
<tr>
<td>{{input["tp"]}}</td>
<td>{{input["amount"]}}</td>
<td>{{input["category"]}}</td>
<td>{{input["transacted"]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
解决方案
你能检查一下下面的代码是否可以工作。在您的 css 文件中定义“红色”和“绿色”css 类。
<tbody>
{% for input in inputs %}
<tr>
{% if input["tp"] == 'expense' %}
<td class="red">{{input["tp"]}}</td>
<td class="red">{{input["amount"]}}</td>
<td class="red">{{input["category"]}}</td>
<td class="red">{{input["transacted"]}}</td>
{% else %}
<td class="green">{{input["tp"]}}</td>
<td class="green">{{input["amount"]}}</td>
<td class="green">{{input["category"]}}</td>
<td class="green">{{input["transacted"]}}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
推荐阅读
- regex - Kotlin 正则表达式边界匹配不起作用
- list - How can I compare the value of two predicates prolog
- docker - 如何构建一个可以在busybox中运行我的c++程序的docker构建文件?
- python - How to get instagram number of followers using instaload?
- c++ - 如何在 C++ 的控制台中结束输入多行?
- php - 显示登录用户的评论总数( $user_id = get_current_user_id(); )
- microsoft-graph-api - How to filter SharePoint list items by ID using Microsoft Graph Api
- c++ - Access violation when attempting to access object member variables in an array
- c# - 在按不同属性排序后如何按一个属性排序列表?
- r - ggplot 中带有 geom_col 和位置“dodge”的 geom_text