Search code examples
odooodoo-10odoo-9qweb

Sum in tree view odoo 9


I need sum row in tree view, and add where condition if is it possible!

My tree view:

row | field_1 | field_2

1 | 8 | Messi

2 | 8 | Messi

3 | 8 | Ronaldo

4 | 8 | Ronaldo

How sum only for Messi and only for Ronaldo and get result 16

In below span I get 32

<span t-esc="sum(line.field_1 for line in doc.my_ids)" widget="float_time"/>

Any solution?

<span t-esc="Messi"/>  16
<span t-esc="Ronaldo"/>  16

Solution

  • You can group your lines by field_2 and than calculate the sum of field_1 for each player.

    <t t-set="players" t-value="[]"/>
    <t t-foreach="doc.my_ids" t-as="l">
        <t t-set="players" t-value="players+[l.field_2]"/>
    </t>
    <t t-foreach="set(players)" t-as="player">
        <p>
        <span t-esc="player"/>
        <t t-set="sum_goal" t-value=0/>
        <t t-foreach="doc.my_ids" t-as="l">
            <t t-if="player==l.field_2">
                <t t-set="sum_goal" t-value=sum_goal+l.field_1/>
            </t>
        </t>
        <span t-esc="sum_goal"/>
        </p>
    </t>
    

    players is a list of field_2. You must use set() to remove the duplication.