[英]Symfony2 Twig table view sort by alphabetical
I have a 2 table in database and i want to get the view like this. 我在数据库中有一个2表,我想要这样的视图。 It is sorted by alphabetical by name.
它按名称的字母顺序排序。 Let's say that I have 2 max rows only and I have 2 data then the 2 data will be display on the left side.
假设我只有2个最大行,而我有2个数据,那么这2个数据将显示在左侧。 If I have 4 data then the other 2 data will display on the right.
如果我有4个数据,那么其他2个数据将显示在右侧。
In my database, the " Name " is the parent and the Account number and Bank Name are the children of the Name. 在我的数据库中,“ 名称 ”是父级,而帐号和银行名称是该名称的子级。 I have created a query in controller something like this:
我在控制器中创建了如下查询:
$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();
this is my twig: 这是我的树枝:
{% 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 %}
I've been trying to work this out and I got stuck. 我一直在努力解决这个问题,但被卡住了。 thanks in advance for the help.
先谢谢您的帮助。
This is the logic that may work, 这是可行的逻辑,
{% 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 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
will give current index and slice(start,length)
will display rows from index start
upto given length
loop.index
将给出当前索引, slice(start,length)
将显示从索引start
到给定length
I didn't tried this, but logic may work. 我没有尝试过,但是逻辑可能有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.