Html table 迭代时使用rowspan

Html table 迭代时使用rowspan,html-table,jinja2,Html Table,Jinja2,假设我有以下两个列表: names = ['Josh', 'Brad', 'Jordan'] favorite_colors = [['blue', 'red'], ['purple', 'gold', 'yellow'], ['green', 'pink']] 也就是说,乔希最喜欢的颜色是蓝色和红色,布拉德最喜欢的颜色是紫色和金色,等等 我通过flask将这些数据传递给一个jinja2模板,并希望将其全部放到一个表中。我希望桌子看起来像这样: 如您所见,我正在使用rowspan属性设置组名和

假设我有以下两个列表:

names = ['Josh', 'Brad', 'Jordan']
favorite_colors = [['blue', 'red'], ['purple', 'gold', 'yellow'], ['green', 'pink']]
也就是说,乔希最喜欢的颜色是蓝色和红色,布拉德最喜欢的颜色是紫色和金色,等等

我通过flask将这些数据传递给一个jinja2模板,并希望将其全部放到一个表中。我希望桌子看起来像这样:

如您所见,我正在使用
rowspan
属性设置组名和颜色。但是,当我尝试使用下面的jinja2代码迭代数据时,
标记会出现在每一行中(我需要它只在第一次出现新名称时出现)

金甲2号:

<div class="table-responsive">
  <table class="table table-bordered table-hover">
    <thead>
      <tr>
        <th>Name</th>
        <th>Favorite Color</th>
      </tr>
    </thead>
    <tbody>
      {% for name_index in range(names | count) %}
        {% for color_index in range(favorite_colors[name_index] | count %}
          <tr>
            <td rowspan="{{ favorite_colors[name_index] | count }}">{{ names[name_index] }}</td>
            <td>{{ favorite_colors[name_index][color_index] }}</td>
          </tr>
        {% endfor %}
      {% endfor %}
    </tbody>
  </table>
<div

名称
喜爱的颜色
{name_索引在范围内的百分比(name|count)%}
{范围内颜色索引的百分比(常用颜色[名称索引]|计数%}
{{names[name_index]}
{{最喜欢的颜色[名称索引][颜色索引]}
{%endfor%}
{%endfor%}
您可以使用检查当前循环是否为第一循环。在您的情况下,可以使用
循环。第一循环可以使用:


名称
喜爱的颜色
{name_索引在范围内的百分比(name|count)%}
{%用于最喜爱的颜色[名称索引]}
{%if loop.first%}
{{names[name_index]}
{%endif%}
{{color}}
{%endfor%}
{%endfor%}
可用于检查当前循环是否为第一循环。在您的情况下,可使用
循环。第一循环可用于:


名称
喜爱的颜色
{name_索引在范围内的百分比(name|count)%}
{%用于最喜爱的颜色[名称索引]}
{%if loop.first%}
{{names[name_index]}
{%endif%}
{{color}}
{%endfor%}
{%endfor%}