【问题标题】:insert row in table with twig用树枝在表格中插入行
【发布时间】:2019-07-18 03:45:42
【问题描述】:

我使用我的代码来填充表格: 我在县牢房和区牢房有两条记录 这是我的代码:

<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             |
+----------+---------+----------+---------------+

如何解决这个问题?

注意:请原谅我的英语,

提前谢谢你

【问题讨论】:

  • 在返回这些数据时是否使用 distinct?我认为问题在于您的 PHP 或 MySQL 查询,而不是树枝。您的视图代码很好
  • @habibun 不,我的查询中没有使用 distinct
  • 您是否尝试过使用表格边框?
  • @habibun 是的,我使用了表格边框,我在县单元格和地区单元格中有两条记录(我的第一个表格)

标签: symfony twig symfony-2.1


【解决方案1】:

您只需将for-loops 放在不同的位置即可读出您的数据,例如

<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2016-02-03
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多