You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 14树形视图头部添加自定义按钮(继承模板方式)

Odoo 14树形视图头部添加自定义按钮问题

我是一名Odoo 14初级开发人员,需要在树形视图头部的“Create”按钮旁添加自定义按钮,且必须通过继承模板(使用xpath)实现。我尝试了如下代码:

<record id="view_stock_inventory_tree" model="ir.ui.view">
    <field name="name">stock.inventory.tree.view</field>
    <field name="model">stock.inventory</field>
    <field name="inherit_id" ref="stock.view_inventory_tree"/>
    <field name="arch" type="xml">
        <xpath expr="//tree" position="inside">
            <header>
                <button name="create_button" type="object" class="oe_highlight" string="Custom Button"/>
            </header>
        </xpath>
        <field name="state" position="after">
            <field name="closure_id"/>
            <field name="cost_currency_id" invisible="1"/>
            <field name="valuation" widget='monetary' options="{'currency_field': 'cost_currency_id'}"/>
        </field>
    </field>
</record>

但该代码未在目标位置显示按钮,移除header标签后按钮会出现在每条记录右侧,不符合需求。请问如何通过继承模板实现将按钮添加到“Create”按钮旁?


解决方案

你需要精准定位到树形视图头部的按钮容器或直接匹配默认“Create”按钮,再插入自定义按钮,以下是两种可行实现方式:

方式1:定位树形视图的header节点插入按钮

如果原视图未显式定义header节点,可通过xpath定位到tree标签下的header(无则自动创建),将按钮插入其中:

<record id="view_stock_inventory_tree" model="ir.ui.view">
    <field name="name">stock.inventory.tree.view</field>
    <field name="model">stock.inventory</field>
    <field name="inherit_id" ref="stock.view_inventory_tree"/>
    <field name="arch" type="xml">
        <!-- 定位tree下的header节点,插入自定义按钮 -->
        <xpath expr="//tree/header" position="inside">
            <button name="create_button" type="object" class="oe_highlight" string="Custom Button"/>
        </xpath>
        <!-- 保留原有字段修改逻辑 -->
        <field name="state" position="after">
            <field name="closure_id"/>
            <field name="cost_currency_id" invisible="1"/>
            <field name="valuation" widget='monetary' options="{'currency_field': 'cost_currency_id'}"/>
        </field>
    </field>
</record>

方式2:直接匹配“Create”按钮并在其后插入

通过xpath精准匹配默认“Create”按钮(name="create"),使用position="after"将自定义按钮放在它右侧:

<record id="view_stock_inventory_tree" model="ir.ui.view">
    <field name="name">stock.inventory.tree.view</field>
    <field name="model">stock.inventory</field>
    <field name="inherit_id" ref="stock.view_inventory_tree"/>
    <field name="arch" type="xml">
        <!-- 定位默认Create按钮,在其后插入自定义按钮 -->
        <xpath expr="//button[@name='create']" position="after">
            <button name="create_button" type="object" class="oe_highlight" string="Custom Button"/>
        </xpath>
        <!-- 保留原有字段修改逻辑 -->
        <field name="state" position="after">
            <field name="closure_id"/>
            <field name="cost_currency_id" invisible="1"/>
            <field name="valuation" widget='monetary' options="{'currency_field': 'cost_currency_id'}"/>
        </field>
    </field>
</record>

注意事项

  • 确保自定义按钮的name对应stock.inventory模型中定义的同名方法(如def create_button(self): ...),否则点击会触发报错。
  • 方式1中若原视图无header节点,Odoo会自动创建该节点并将按钮加入顶部工具栏。

内容的提问来源于stack exchange,提问作者Juan Luis Andián Tápiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 18:12:45