Odoo Help

1

How do you use variables in a Qweb loop?

By
Jeff Beidler
on 11/10/14, 5:01 PM 13,960 views

This page of the documentation describes variables that can be used in a t-foreach loop in Qweb, but doesn't provide any examples of how to access those variables.  I was thinking that I could use the $as_index variable to list line numbers on a sales order, but everything I have tried causes an error.  Can anyone provide an example of how to output the index?  Or, even better, the index + 1, since it starts at zero?

8
Sajin Aziz
On 11/12/14, 4:32 AM

try this code inside table,

 <t t-set="i" t-value="1"/>

<tr t-foreach="o.order_line" t-as="l">
                        <td> <span t-esc="i"/> </td>

    <t t-set="i" t-value="i+1"/>
</tr>

That's what I came up with, too. Just wanted to know how to do it in the manner described in the "documentation".

Jeff Beidler
on 11/12/14, 1:29 PM
2
Jeff Beidler
On 11/12/14, 7:18 PM

I got the $as_index variable to work.  Here is an example:

<tr t-foreach="o.order_line" t-as="l">
                        <td>
                            <span t-esc="l_index + 1" />
. . .

The index is zero-based, hence the addition of 1.  Hope this helps.

-1
Antony Lesuisse (al)
On 11/12/14, 6:41 AM

var_index is automatically defined

Check https://www.odoo.com/documentation/8.0/reference/qweb.html#loops

Yes, that is the page I linked in my question. What I was hoping for was an example of usage. Everything I tried resulted in an error.

Jeff Beidler
on 11/12/14, 1:32 PM

@Antony Lesuisse, Thanks for the link. I'm currently building a community module to add multiple product images for use in the website e-commerce pages here: https://github.com/OdooCommunityWidgets/website_multi_image unfortunately i've only just begun playing around with Odoo development and I have hit a brick wall trying to figure out how to create a t-foreach loop to output all images in the one2many in this module. Do you have any suggestions on how I could achieve this in 8.0?

Luke Branch
on 12/4/14, 6:16 AM
https://www.odoo.com/documentation/8.0/reference/qweb.html

Use the doc Luke ! :)

On 12/04/2014 12:16 PM, Luke wrote:
> @Antony Lesuisse, Thanks for the link. I'm currently building a community
> module to add multiple product images for use in the website e-commerce pages
> here: https://github.com/OdooCommunityWidgets/website_multi_image
> unfortunately i've only just begun playing around with Odoo development and I
> have hit a brick wall trying to figure out how to create a t-foreach loop to
> output all images in the one2many in this module. Do you have any suggestions
> on how I could achieve this in 8.0?
>
> --
> Luke
> Sent by Odoo S.A.  using Odoo 
> about Forum Post False 
Antony Lesuisse (al)
on 12/9/14, 4:44 PM
-1
Martin Pishpecki
On 12/24/14, 7:14 AM

I have tryed this:

<t t-set="i" t-value="1"/>
<tr t-foreach="o.order_line" t-as="l">
                        <td> <span t-esc="i"/> </td>
<t t-set="i" t-value="i+1"/>
</tr>

When the loop finishes, the ounter resets to 1. Does anyone kow a way to update a variable outside the loop? I would like to define a variable before the loop and set it to false. If I find what I am looking for inside the loop I would like to set the value of the variable to True. This var would later be used as a condition if a div is visible or not.

Any help is appreciated. Thx

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 11/10/14, 5:01 PM
Seen: 13960 times
Last updated: 1/17/17, 2:12 PM