【问题标题】:Twig: Finding the sum of values of rows in columnTwig:查找列中行的值的总和
【发布时间】:2014-05-01 17:53:46
【问题描述】:

如何使用 Twig 找到列中所有行的总值?例如,我有一列“QTY”,它将列出每行的数量,我想要 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 列或任何列的计数。

【问题讨论】:

  • 这真的是 Twig 的工作吗?
  • QTY 的总 ROWS 之和”是什么意思?您是指 QTY 列中的行数吗?您是指 QTY 列每行内的值的总和吗?
  • @Javad QTY 列的行数。
  • 你能把你如何在twig中创建表格的代码放上去吗?这将有助于解决您的问题

标签: sum logic twig


【解决方案1】:

根据您的代码并根据您想要获取 Qty 列中的行数您可以尝试

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

如果您想在某处显示该数量(例如跨度),您可以在之后使用&lt;span&gt;{{ row_count }}&lt;/span&gt;

更新

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

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

【讨论】:

  • 它给了我一个错误,模板中不存在“row_count”。
  • 您将row_count 放在代码中的什么位置来显示?
  • 首先我把它放在桌子上方,它给了我错误,但是当我把它移到桌子下面时,它仍然给了我错误。
  • @Noobtastic 我有一个更好的建议;您可以在任何地方显示行数;刚刚更新了我的建议,你可以看到。
【解决方案2】:

如果您在 twig 中打印表格的行并想要获取“QTY 列中的行数”,您应该通过对行数组执行简单的 count() 将此数据传递给 twig。如果您想以“艰难的方式”做到这一点,您可以这样做:

{% set numRows = 0 %}

{% for .... %}

{% set numRows = numRows + 1 %}

{% endfor %}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多