You need to enable JavaScript to run this app. Odoo 13 如何在Tree View上方添加按钮绑定自定义函数
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 13 如何在Tree View上方添加按钮绑定自定义函数

Odoo Tree View顶部添加自定义按钮实现方案

结论:该需求可通过Odoo原生能力直接实现,无需额外hack或替代方案

实现步骤

  1. 修改原有Tree视图配置,添加按钮放置区
    原有的视图arch部分调整如下,在<tree>标签内部开头新增<header>节点放置自定义按钮,按钮会自动渲染在Tree视图顶部工具栏,和原生「创建」「导入」按钮并排:
    <record id="account_payment_import_view_tree" model="ir.ui.view">
        <field name="name">account.payment.import.view.tree</field>
        <field name="model">account.payment.import</field>
        <field name="arch" type="xml">
            <tree string="Payment Imports" decoration-info="payment_id != False" decoration-danger="error_msg != False">
                <!-- 新增顶部按钮区 -->
                <header>
                    <button 
                        name="action_process_payment_import" 
                        type="object" 
                        string="执行匹配校验" 
                        class="btn-primary"
                        confirm="是否确定要执行支付单匹配操作?"
                    />
                </header>
                <!-- 原有字段保持不变 -->
                <field name="transaction_date"/>
                <field name="facts_id"/>
                <field name="paid_in_lei"/>
                <field name="paid_in_euro"/>
                <field name="amount"/>
                <field name="account"/>
                <field name="account_no"/>
                <field name="document_no"/>
                <field name="details_bk_statement"/>
                <field name="error_msg"/>
                <field name="invoice_number" invisible="1"/>
                <field name="payment_id" widget="many2onebutton" invisible="1"/>
                <field name="invoice_id" widget="many2onebutton" invisible="1"/>
                <field name="company_id" invisible="1"/>
                <field name="currency_id" invisible="1"/>
            </tree>
        </field>
    </record>
    
  2. 在对应模型中实现自定义触发函数
    在account.payment.import模型的逻辑文件中新增方法,方法名和按钮name属性保持一致:
    from odoo import models, _
    
    class AccountPaymentImport(models.Model):
        _inherit = 'account.payment.import'
    
        def action_process_payment_import(self):
            # 直接编写业务逻辑,self自动携带当前操作的记录集
            # 无选中记录时:self为当前列表domain下的所有符合条件记录
            # 有选中记录时:self为用户勾选的选中记录
            for record in self:
                # 示例:执行支付单生成、发票匹配等自定义逻辑
                if not record.error_msg and not record.payment_id:
                    pass
            
            # 执行完成后返回操作提示(可选)
            return {
                'type': 'ir.actions.client',
                'tag': 'display_notification',
                'params': {
                    'title': _('操作完成'),
                    'message': _('共处理%s条支付导入记录') % len(self),
                    'type': 'success',
                }
            }
    

兼容旧版本说明

  • 若使用的Odoo版本早于13.0,不支持Tree视图内的<header>节点,可使用替代方案:
    • 方法1:将自定义按钮挂载到列表视图右上角的「动作」下拉菜单中,通过给模型新增ir.actions.server动作实现,点击后同样会触发指定函数
    • 方法2:将视图的view_mode调整为tree,form,把按钮放在Form视图的header中,同时配置Form视图仅在按钮触发时打开,不影响原有列表的使用
  • 原有配置中的context={'edit': 0}不会影响按钮触发逻辑,无需修改
  • 按钮可按需调整样式:添加icon="fa-refresh"显示图标、修改class为btn-secondary调整配色、配置groups属性按用户权限控制显示

注意:按钮的type="object"属性是触发模型内置方法的关键,不要修改为其他类型。

内容的提问来源于stack exchange,提问作者Matt Pedrosa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.28 21:24:03