我有一个包含10行和10列int值的表;我需要在第11行打印总数,我也需要总计并在列打印。 示例:
cell(1x11) = row1+row2+row3...row10;
cell(2x11) = row1+row2+row3...row10;
和
cell(11x1) = col1+col2+col3...col10;
cell(11x2) = col1+col2+col3...col10;
我怎样才能轻松做到这一点。
必填结果:
<table>
<tr>
<th></th>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Total</th>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<th>Total</th>
<td>3</td>
<td>6</td>
<td>9</td>
<td>18</td>
</tr>
</table>
我实际的树枝代码: `
{% set len = entities|length %}
{% set len_e = exams|length %}
{% if len_e != null and len_e is not empty and len_e > 0 %}
<div class="table-responsive">
<table id="FZUserExam" class="table table-striped table-bordered">
<thead>
<tr>
<th rowspan="2">SUBJECT</th>
<th rowspan="2">Max<br/>Marks</th>
{% for i in 1..len_e %}
{% if i == 1 %}
{% set terms = exams[0].term %}
{% set x = 1 %}
{% else %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
{% else %}
<th colspan="{{x+1}}">{{ terms }}</th>
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% endif %}
{% if i == len_e %}
<th colspan="{{x+1}}">{{ terms }}</th>
{% endif %}
{% endfor %}
<th rowspan="2">Grand<br/>Total</th>
</tr>
<tr>
{% set terms = exams[0].term %}
{% set x = 1 %}
{% for i in 1..len_e %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
<th>{{ exams[i-1].exam }}</th>
{% else %}
<th>Total</th>
<th>{{ exams[i-1].exam }}</th>
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% if i == len_e %}
<th>Total</th>
{% endif %}
{% endfor %}
</tr>
</thead>
<tbody>
{% set y = 0 %}
{% set total = 0 %}{% set gtotal = 0 %}
{% for a,s in subject %}
<tr>
<td>{{ s.name }}</td>
<td>{{ s.maxMarks }}</td>
{% set terms = exams[0].term %}
{% set x = 1 %}
{% for i in 1..len_e %}
{% set name = exams[i-1].term %}
{% if name == terms %}
{% set x = x+1 %}
{% if entities %}
{% if s.id == entities[y].subjects.id and entities[y].exams.id == exams[i-1].id %}
<td class="fz_t_e" data-val="{{ entities[y].marks }}" data-id="{{entities[y].id}}" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">
{{ entities[y].marks }}
</td>
{% set total = total + entities[y].marks %}
{% if len > (y+1) %}
{% set y = y+1 %}
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% else %}
<td>{{ total }}{% set gtotal = gtotal+total %}{% set total = 0 %}</td>
{% if entities %}
{% if s.id == entities[y].subjects.id and entities[y].exams.id == exams[i-1].id %}
<td class="fz_t_e" data-val="{{ entities[y].marks }}" data-id="{{entities[y].id}}" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">
{{ entities[y].marks }}
</td>
{% set total = total + entities[y].marks %}
{% if len > (y+1) %}
{% set y = y+1 %}
{% endif %}
{% endif %}
{% else %}
<td class="fz_t_e" data-val="null" data-id="null" data-exams="{{exams[i-1].id}}" data-student="{{student.id}}" data-subject="{{s.id}}">-</td>
{% endif %}
{% set terms = exams[i-1].term %}
{% set x = 1 %}
{% endif %}
{% if i == len_e %}
<td>{{ total }}{% set gtotal = gtotal+total %}{% set total = 0 %}</td>
<td>{{ gtotal }}{% set gtotal = 0 %}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
<tr class="totalColumn">
<td>Total</td>
{% for i in 1..len_e+4 %}
<td></td>
{% endfor %}
</tr>
{#<tr class="totalGrade">
<td>Grade</td>
{% for i in 1..len_e+4 %}
<td></td>
{% endfor %}
</tr>#}
</tbody>
</table>
</div>
{% else %}
<h3 class="text-center">Exam Table Not Updated</h3>
{% endif %}
</div>`
答案 0 :(得分:1)
我不知道你用什么数据结构来记住你的整数,但你可以从这样的东西开始:
<table>
{% for r in rows %}
<tr>
{% set rowTotal = 0 %}
{% for c in r.columns %}
{% set rowTotal = rowTotal + c.value %}
<td>{{ c.value }}</td>
{% endfor %}
<td>{{ rowTotal }}</td>
</tr>
{% endfor %}
</table>