用细枝在表中插入行

时间:2019-02-24 17:04:43

标签: symfony twig symfony-2.1

我用我的代码填充表:  我在县区和区区有两个记录 这是我的代码:

<table>
    <tr>
    <th>town</th>
    <th>counties</th>
    <th>districts</th>
    <th>number doctor</th>
    </tr>
    {% for item in locality %}
        <tr>
          <td>{{ item.name }}</td>

          <td>
            {% for list in item.regions %}
                {{ list.county}}
            {% endfor %}
          </td>

           <td>
            {% for list in item.regions %}
                {{ list.code}}
            {% endfor %}
           </td>

          <td>{{ item.regions|default([])|length }}</td>
        </tr>
    {% endfor %}
</table>

这是我的桌子:

+--------+---------+----------+---------------+
|   town |counties |districts | number doctor |
+--------+---------+----------+---------------+
| Adan   | Afla    | avo      |               |
|        | kent    | joly     | 2             |
+--------+---------+----------+---------------+

但是,我希望表格显示变为:

+----------+---------+----------+---------------+
| town     |counties |districts | number doctor |
+----------+---------+----------+---------------+
| Adan     | Afla    |   avo    | 2             |
+----------+---------+----------+---------------+
| Adan     | kent    |   joly   | 2             |
+----------+---------+----------+---------------+

如何解决此问题?

注意:请问我的英语,

先谢谢您

1 个答案:

答案 0 :(得分:2)

例如,您只需要放置for循环以读取数据即可,例如

<table>
    <tr>
    <th>town</th>
    <th>counties</th>
    <th>districts</th>
    <th>number doctor</th>
    </tr>
{% for item in locality %}
    {% for list in item.regions|default([]) %}
    <tr>
        <td>{{ item.name }}</td>
        <td>{{ list.county}}</td>
        <td>{{ list.code}}</td>
        <td>{{ item.regions|length }}</td>
    </tr>
    {% endfor %}
{% endfor %}
</table>

demo