Pointer
Pointer

Reputation: 2186

Add sum in qweb report

How add SUM for etc. id field bottom on table without .py file and function?

                    <table class="table table-condensed">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Quantity</th>
                            </tr>
                        </thead>
                        <tr>
                            <t t-foreach="docs" t-as="o">
                                <tr>
                                    <td><t t-esc="o.id"/></td>
                                    <td><t t-esc="o.name"/></td>
                                    <td><t t-esc="o.quantity"/></td>
                                </tr>
                            </t>
                        </tr>
                        <tr>
                            <td colspan="3" class="text-right"> SUM (o.quantity) </td>
                        </tr>
                    </table>  

Is it possible in this situation?

Upvotes: 1

Views: 6157

Answers (2)

Hilar AK
Hilar AK

Reputation: 1675

try below code

 <table class="table table-condensed">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Quantity</th>
                        </tr>
                    </thead>
                    <tr>
                        <t t-foreach="docs" t-as="o">
                            <tr>
                                <td><t t-esc="o.id"/></td>
                                <td><t t-esc="o.name"/></td>
                                <td><t t-esc="o.quantity"/></td>
                            </tr>

                        </t>
                    </tr>
                    <tr>
                        <span t-esc="sum(line.quantity for line in docs)"/>
                    </tr>
                </table>  

Upvotes: 3

You can create one variable and sum quantity in every loop iteration and print it at last.

So you should try as following :

<table class="table table-condensed">
    <t t-set="qty" t-value="0">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Quantity</th>
        </tr>
    </thead>
        <tr>
            <t t-foreach="docs" t-as="o">
            <tr>
                <td><t t-esc="o.id"/></td>
                <td><t t-esc="o.name"/></td>
                <td><t t-esc="o.quantity"/></td>
                <t t-set="qty" t-value="qty + o.quantity"/>
            </tr>
            </t>
            </tr>
            <tr>
                <td colspan="3" class="text-right"> <span t-esc="qty"></span> </td>
            </tr>
</table>

Upvotes: 2

Related Questions