Symfony2 Twig表视图按字母顺序排序

时间:2014-12-01 05:41:50

标签: php symfony twig

我在数据库中有一个2表,我希望得到这样的视图。它按名称按字母顺序排序。假设我只有2个最大行,而我有2个数据,那么2个数据将显示在左侧。如果我有4个数据,那么另外2个数据将显示在右侧。

  

enter image description here

在我的数据库中,“名称”是父级,帐号银行名称是姓名的子级。我在控制器中创建了一个像这样的查询:

$query = $em->createQuery(
         "SELECT
             e.id, e.name,
             eb.numId, eb.bankName, eb.bankAcct
          FROM Bundle:table1 e
          LEFT JOIN Bundle:table2 eb
          WITH e.id = eb.numId
          ORDER BY e.name ASC"
         );
$entity = $query->getArrayResult();

这是我的树枝:

{% for list in entity|batch(1) %}
   <tr>
    {% for column in list %}
        <td>{{ column.entityName }}</td>
        <td>{{ column.bankName }}</td>
        <td class="bank-acct">{{ column.bankAcct }}</td>
    {% endfor %}
   </tr>
{% endfor %}

我一直试图解决这个问题而且我遇到了困难。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是可行的逻辑,

显示第一个div(左侧)

{% set CurrentIndex = '' %} 
    <div class="1">
    <table>
        {% for i in [1, 2, 3, 4, 5]|slice(0, 2) %}
            {# will iterate over 0 and 1 element #}
      <tr>
          {% if loop.index < 2 %}
              #Display table columns

      #set Current index at which loop will break, so that we can use it in next iteration
              {% set CurrentIndex = loop.index %} 
          {% endif %}
      </tr>    
        {% endfor %}

    </table>
    </div>

显示第二个div(右侧)

    <div class="2">
    <table>
        {% for i in [1, 2, 3, 4, 5]|slice(CurrentIndex, 2) %}
            {# will iterate over 0 and 1 element #}
      <tr>
          {% if loop.index < 2 %}
              #Display table columns
          {% endif %}
      </tr>    
        {% endfor %}

    </table>
    </div>

loop.index将提供当前索引,slice(start,length)将显示从索引start到给定length的行

我没有尝试过,但逻辑可能有用。

链接 - Sliceset

相关问题