Domanda

How do I find the total value of all the rows in a column with Twig? For example I have a column "QTY" that will list number of quantities of each row, I want the sum of the total ROWS of QTY (not the sum of qty). What is the tag/logic in Twig?

I have something like this:

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

For the rows that populate, I would like the count of the column of QTY, or any column.

È stato utile?

Soluzione

Base on your code and according to that you want to get Count of rows in Qty column you can try

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

if you want to show that amount somewhere (like a span) you can use <span>{{ row_count }}</span> after

UPDATED

A better solution to show the rows count anywhere if your twig template might be just showing the count of getbatches:

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

Altri suggerimenti

If you print rows of a table in twig and wany to get the "count of rows in QTY column", you should pass this data to twig from doing a simple count() on the array of rows. If you want to do it "the hard way", you could do:

{% set numRows = 0 %}

{% for .... %}

{% set numRows = numRows + 1 %}

{% endfor %}

but as @TheLittlePig said, twig's purpose is to display data, not do calculations

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top