【问题标题】:change icon color based on value of SQL table in flask根据烧瓶中 SQL 表的值更改图标颜色
【发布时间】:2021-09-17 01:03:48
【问题描述】:

我使用 .py 代码在 html 中显示表格:

@flask.route('/users_table', methods=['GET', 'POST'])
def users_table():
    cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cur.execute('SELECT * FROM `users` WHERE isadmin = 0')
    all_users = cur.fetchall()
    return render_template('users-table.html', all_users=all_users

和.html代码:

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
    <tr>
        <th>User Name</th>
        <th>Email</th>
        <th>Company</th>
        <th>Status</th>
        <th>Tools</th>
    </tr>
</thead>
      {% for row in all_users %}
    <tr>
        <td>{{row.username}}</td>
        <td>{{row.email}}</td>
        <td>{{row.company_name}}</td>
        <td>{{row.isenabled}}</td>
        <td><a href="/reset_user_pass/{{row.Id}}" data-toggle="modal" data- 
        target="#modaledit{{row.Id}}"><i class="fa fa-play"></i></a></td>
    </tr>
    {% endfor %}
</table>

show sql table

我想根据 'status' 列的值更改 'fa fa-play' 图标。

我怎样才能做到这一点

【问题讨论】:

  • 你想要哪个图标代表哪个状态?

标签: python mysql python-3.x flask


【解决方案1】:

我猜你可以使用条件语句。

<td><a href="/reset_user_pass/{{row.Id}}" data-toggle="modal" data- 
        target="#modaledit{{row.Id}}"> 
{% if row.status == 0  %}
    <i class="fa fa-play"></i></a> 
{% else %}
    <i class="SOMETHING ELSE"></i></a>
{% endif %}
</td>

您还可以签出三元条件。

【讨论】:

    【解决方案2】:
    <i class="fa fa-{{ 'icon1' if row.isenabled else 'icon2' }}"></i>
    

    假设状态值是根据row.isenabled确定的,您可以使用内联if/else根据条件显示不同的图标。我使用了icon1icon2,你可以用真实的图标替换它们。

    【讨论】:

    • 感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2022-12-15
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多