تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
10746 أدوات العرض

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

المنشورات ذات الصلة الردود أدوات العرض النشاط
2
مارس 24
1305
0
سبتمبر 23
4995
1
أغسطس 17
9661
1
مارس 15
5938
11
أبريل 23
92140