This question has been flagged
11 Replies
87689 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?

Avatar
Discard
Best Answer

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>

Avatar
Discard
Author

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

Author Best Answer

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.

Avatar
Discard
Best Answer

This is pretty simple and straight forward.

When we define our loop like this:

<code>

<t t-foreach="people" t-as="person">

<tr>

<th scope="row"><span t-raw="person_index"/></th>

<td><span t-field="person.name"/></td>

<td><span t-field="person.age"/></td>

<td><span t-raw="person.weight"/></td>

</tr>

</t>

</code>

To get the current index/position of the person with the loop you can do person_index. i.e your variable_index or any other available variable withing the context of qweb loop.

Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

var_index is automatically defined

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

Avatar
Discard
Author

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.

@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?

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