Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
10728 มุมมอง

Is it possible get variable value from loop?

For example:

            <t t-set="flag" t-value="False"/>
            <!-- flag now False -->
            <tr t-foreach="o.lines" t-as="line">
                <td><span t-field="line.name"/></td>
                <td><span t-field="line.qty"/></td>
                <t t-set="flag" t-value="True"/>
                <!-- flag now True -->
            </tr>
            <!-- flag again False -->


Variable flag inside loop is different from variable flag outide. How send information outside loop?

อวตาร
ละทิ้ง

A patch and some ideas on how to calculate totals have been added to the github issue. https://github.com/odoo/odoo/issues/4461#issuecomment-72199618

คำตอบที่ดีที่สุด

There is a GitHub Issue #4461 on this problem.

UPDATE: A patch to v8 was released and some good programming suggestions were made by Olivier on how to avoid using loops.

อวตาร
ละทิ้ง

Updated my answer with the patch.

ผู้เขียน คำตอบที่ดีที่สุด

Thank you Stephen Mack !

As a workaround we can use:

1) If you can insert output in last iteration then use like:

                <t t-set="total" t-value="0" />
                <t t-foreach="o.order_line" t-as="line">
                    <t t-set="total" t-value="total+line.quantity" />
                    <t t-if="line_last">
                        <t t-esc="total" />
                    </t>
                </t>

​​2) If you can't insert output in loop then use like:

                <t t-set="total" t-value="sum([line.quantity for line in o.order_line])" />

อวตาร
ละทิ้ง

Those are some ugly workarounds. But thanks for posting them.

ผู้เขียน

I agree :(

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
มี.ค. 24
1283
0
ก.ย. 23
4977
1
ส.ค. 17
9651
1
มี.ค. 15
5884
How do you use variables in a Qweb loop? แก้ไขแล้ว
11
เม.ย. 23
92077