Odoo Help

Welcome!

This community 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.

1

Qweb order array before iterating with foreach

By
Phillip
on 9/10/15, 7:52 AM 2,558 views

I have an array of values which I can use the 

<t t-foreach="docs" t-as="doc">

function. Is there a simple method similar to how you would order an array in python or javascript which I have access to in the Qweb context?

Here is a summary of the code and its printout.

(THIS IS NOT REAL CODE JUST FOR DEMONSTRATION OF CONCEPT)

<t t-esc="doc.field"/> <br/>
<t t-foreach="doc.field" t-as="item">
     <t t-esc="item"/> <br/>
     <t t-esc="item.year"/>
     <br/> <br/> <br/>
</t>

THIS CODE OUTPUTS THIS

model.field(1, 2, 3, 4, 5, 6)

model.field(1,)

1899

model.field(2,)

2015

model.field(3,)

2010

model.field(4,)

2012

model.field(5,)

2011

model.field(6,)

2014

As you can see the years are not in the correct order. What I would like to do would be.

<t t-set="newList" t-value="sorted(doc.field, key=lambda k: k['year'])"/>
<t t-foreach="newList" t-as="newItem">


However I get an error. I think the object none type or something like that.




I have an iterable object which is accessible from my report. The problem is that if the user puts the records into the system in the wrong order the report is also printing the lines in the exact same WRONG order. I want to order the records before iterating with t-foreach.

Phillip
on 9/11/15, 3:02 PM
2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 9/11/15, 12:35 AM

Philip,

You can use python code inside t-esc template.

Which will help you to sort your data.

try this:

<t t-esc="docs.sort()"/>

or if it does not work than you can use parser to do any changes related to your data runtime.

Hope this might help you.

Rgds,

Anil.



Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/10/15, 7:52 AM
Seen: 2558 times
Last updated: 9/11/15, 4:17 PM