【发布时间】:2019-12-31 18:51:11
【问题描述】:
我想在处理表格时计算总和,所以渲染是逐行进行的,但我需要对列进行总计,我需要每列一个计数器。
{% assign cols = "col1,col2" | split: "," %}
{% assign rows = "row1,row2" | split: "," %}
{% assign total = ????? %}
...
{% for row in rows %}
<tr>
{% for col in cols %}
<td>
{% for post in site.posts %}
{% if post.colThing == col and post.rowThing == row %}
{% assign total[row] = total[row] | plus: post.thatNumber %}
.... {{ post.thatNumber }} ...
{% endif %}
{% endfor %}
</td>
{% endfor %}
</tr>
{% endfor %}
<tr>
{% for col in cols %}
<td>
.... {{ total[row] }} ...
</td>
{% endfor %}
</tr>
我能想到的最好办法是再次解析site.posts 的总数:
...
<tr>
{% for col in cols %}
{% assign total = 0 %}
{% for post in site.posts %}
{% if post.colThing == col %}
{% assign total = total | plus: post.thatNumber %}
{% endif %}
{% endfor %}
<td>
.... {{ total }} ...
</td>
{% endfor %}
</tr>
这是获得总数的最有效方法吗?
【问题讨论】: