简体   繁体   English

Jinja 表,当列 == 值时,然后将 div 添加到特定列

[英]Jinja table, when column == value then add div to specific column

I have a flask app with a jinja table, one of the columns has the value = 1 when that row meets a criteria.我有一个带有 jinja 表的 flask 应用程序,当该行满足条件时,其中一列的值 = 1。

For every row that a 1 is present in that column i would like to replace it with a circle, for example:对于该列中存在 1 的每一行,我想用圆圈替换它,例如:

在此处输入图像描述

How would I add it to my jinja table?我如何将它添加到我的 jinja 表中?

<table>
        <thead>

          <tr>
            {% for col in column_names %}
            <th>
            
              {{col}}
             
            </th>
            {% endfor %}
          </tr>

        </thead>
        <tbody>
          {% for row in row_data %}
          <tr>
            {% for col, row_ in zip(column_names, row) %}

  {% if loop.index == 1 %} 
            <td> 

IF  {{ row[16] }} == 1 then <div class="circle" style="float: left;">LB</div> 
else ''
end 

 {{row_}}</td>
{% else %}
            <td>{{row_}}</td>

 {% endif %}

            {% endfor %}
          </tr>
          {% endfor %}

         
        </tbody>
  
      </table>

HTML/CSS code of div i want to appear in that cell我想出现在该单元格中的 div 的 HTML/CSS 代码

.circle
    {
    width:20px;
    height:20px;
    border-radius:10px;
    font-size:8px;
    color:#fff;
    line-height:20px;
    text-align:center;
    background:#000
    }

<div class="circle" style="float: left;">LB</div> 

Solution解决方案

Try the following.尝试以下操作。

The one-line if-else statement in jinja2 looks like this: jinja2中的单行if-else语句如下所示:

 {{ OUTPUT_WHEN_TRUE if condition else OUTPUT_WHEN_FLASE }}

So, in your case, the code within for each <td></td> (where loop.index == 1 over the inner loop) will look like this:因此,在您的情况下,每个<td></td>中的代码(内部循环中的loop.index == 1 )将如下所示:

 {{ '<div class="circle" style="float: left;">LB</div>' if row[16] == 1 else '' }} {{ row_ }}

在 Colab 中打开

Code代码

<table>
    <thead>

        <tr>
        {% for col in column_names %}
        <th>
        
            {{col}}
            
        </th>
        {% endfor %}
        </tr>

    </thead>
    <tbody>
        {% for row in row_data %}
        {% set row_loop = loop %}
        <tr>
        {% for col, row_ in zip(column_names, row) %}
        {% set col_loop = loop %}

        {# Choose which loop: row or col you are referring to #} 
        {% if col_loop.index == 1 %} 
        <td> 

            {{ '<div class="circle" style="float: left;">LB</div>' if row[16] == 1 else '' }} {{ row_ }}

        </td>
        {% else %}
            
            <td>{{ row_ }}</td>

        {% endif %}

        {% endfor %}
        </tr>
        {% endfor %}
     
    </tbody>
  
</table>

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM