如何使用t-if在行号中建立条件

时间:2019-01-24 15:01:36

标签: odoo qweb

我尝试在odoo 11中修改报表发票中的一行,我需要有关单击“打印”按钮进行制作后报表生成的行数的条件

<t-if="number_line_in_table== 1">
   <!--DO something..-->
</t>

1 个答案:

答案 0 :(得分:1)

我认为您无法使用Python风格的enumerate,但是您可以使用老式计数器,例如以下。

<t t-set="counter" t-value="0"/>
<t t-foreach="records" t-as="record">
    <t t-set="counter" t-value="counter + 1"/>
    <t t-if="counter == 1">
        <!-- Do Something. -->
    </t>
</t>

这里是an example的核心人员。

这是QWeb Documentation for Odoo 12,具有更多信息。


对于您的特定示例,您想要继承一个现有视图,以在该视图的现有counter元素中包含一个t-foreach

现有视图的相关部分

<tr t-foreach="o.order_line" t-as="line">
    <td>
        <span t-field="line.name"/>
    </td>
    <td>
        <span t-esc="', '.join(map(lambda x: x.name, line.taxes_id))"/>
    </td>
    <td class="text-center">
        <span t-field="line.date_planned"/>
    </td>
    <td class="text-right">
        <span t-field="line.product_qty"/>
        <span t-field="line.product_uom.name" groups="product.group_uom"/>
    </td>
    <td class="text-right">
        <span t-field="line.price_unit"/>
    </td>
    <td class="text-right">
        <span t-field="line.price_subtotal"
            t-options='{"widget": "monetary", "display_currency": o.currency_id}'/>
    </td>
</tr>

如何使用计数器

免责声明:我以前没有做过这种事情。它可能完全不这样工作。如果无法通过继承使它起作用,则可以根据需要通过replace foreach xpath整个循环。

<xpath expr="//tr[@t-foreach='o.order_line']" position="before">
    <t-set="counter" t-value="0"/>
</xpath>
<xpath expr="//tr[@t-foreach='o.order_line']/td[1]" position="before">
    <t-set="counter" t-value="counter + 1"/>
    <t t-if="counter == 1">
        <!-- Do Something. -->
    </t>
</xpath>