Twig:查找列中行的值的总和

时间:2014-05-01 17:53:46

标签: sum logic twig

如何使用Twig查找列中所有行的总值?例如,我有一个列" QTY"这将列出每行的数量,我想要总的ROWS数量的总和(不是数量的总和)。 Twig中的标签/逻辑是什么?

我有这样的事情:

<table class="tablesorter">
    <thead>
       <th>DA</th>
       <th>Part</th>
       <th>Batch</th>
       <th>Qty</th>
    </thead>
  {% for getbatches in getbatch %}
    <tr>
       <td>{{getbatch.dano}}</td>
       <td>{{getbatch.partno}}</td>
       <td class="highlight">{{getbatch.batchno}}</td>
       <td>{{getbatch.inqty}}</td>
    </tr>
  {% endfor %}
</table>

对于填充的行,我想要QTY列或任何列的计数。

2 个答案:

答案 0 :(得分:2)

根据您的代码,根据您想要获得数量列中的行数,您可以尝试

<table class="tablesorter">
  <thead>
     <th>DA</th>
     <th>Part</th>
     <th>Batch</th>
     <th>Qty</th>
  </thead>
  {% set row_count = 0 %}
  {% for getbatches in getbatch %}
  <tr>
     <td>{{getbatch.dano}}</td>
     <td>{{getbatch.partno}}</td>
     <td class="highlight">{{getbatch.batchno}}</td>
     <td>{{getbatch.inqty}}</td>
  </tr>
  {% set row_count = row_count + 1 %}
  {% endfor %}
</table>

如果你想在某个地方展示这个数量(比如跨度)你可以在

之后使用<span>{{ row_count }}</span>

<强>已更新

如果您的树枝模板可能只显示getbatches的数量,则可以在任何地方显示行计数的更好解决方案:

<span>Row count: </span><span>{{ getbatches is defined ? getbatches|length : 0 }}</span>

答案 1 :(得分:0)

如果您在twig中打印一行表并且想要在QTY列&#34;中获得&#34;行数,则应该通过对行数组执行简单计数()将此数据传递给twig 。如果你想要做到这一点,那么你可以做到:#/ p>。

{% set numRows = 0 %}

{% for .... %}

{% set numRows = numRows + 1 %}

{% endfor %}

但正如@TheLittlePig所说,twig的目的是显示数据,而不是做计算

相关问题