2
votes

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
1
You don't need write code for that. From the view you can group by field_2. So Odoo calculate the sum value of the field_1 for you automaticqvpham
@julivico I need result in qweb, any example?user_odoo
@qvpham You should convert your comment into an answer and include at least an excerpt from the link in case the link dies in the future.travisw
@travisw: i just don't want to copy the solution from someoneqvpham

1 Answers

4
votes

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.