Sudhakar Krishnan
Sudhakar Krishnan

Reputation: 880

Twig : table total all row and columns

I have a table with 10 rows and 10 columns with int values; I need the total to be print at 11th row, also i need to total and print at columns. Example :

cell(1x11) = row1+row2+row3...row10;
cell(2x11) = row1+row2+row3...row10;

and

cell(11x1) = col1+col2+col3...col10;
cell(11x2) = col1+col2+col3...col10;

How can i do this easily.

Required result :

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

My actual twig code: `

            {% 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>`

Upvotes: 0

Views: 1039

Answers (1)

Alessandro Lai
Alessandro Lai

Reputation: 2274

I don't know what data structure you're using to memorize your ints, but you could start with something like this:

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

Upvotes: 1

Related Questions