首页 > 编程语言 >Odoo 自定义form表单按钮点击事件处理程序

Odoo 自定义form表单按钮点击事件处理程序

时间:2023-02-06 21:11:18浏览次数:40  
标签:wizard 自定义 form views self 按钮 attrs Odoo

实践环境

Odoo 14.0-20221212 (Community Edition)

代码实现

方案1

通过研究发现,点击odoo form表单按钮时,会调用odoo14\odoo\addons\web\static\src\js\views\form\form_controller.js文件中的_onButtonClicked函数,在该函数中响应点击事件。所以,我们可以通过重写该方法来实现自定义响应点击事件。示例如下

表单视图定义

codePojects\odoo14\custom\estate\wizards\demo_wizard_views.xml

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <record id="demo_wizard_view_form" model="ir.ui.view">
            <field name="name">demo.wizard.form</field>
            <field name="model">demo.wizard</field>
            <field name="arch" type="xml">
                <form>
                    //...代码略 
                    <footer>
                        <button name="action_confirm" special="other" type="object" string="确认" class="oe_highlight"/>
                        <button string="关闭" class="oe_link" special="cancel"/>
                    </footer>
                </form>
            </field>
        </record>
        //...代码略        
    </data>
</odoo>

重定义web.FormController以实现重写_onButtonClicked

codePojects\odoo14/estate/static/src/js/views/form_controller.js

odoo.define('customModule.FormController', function (require) {
    "use strict";

 	var formController = require('web.FormController');
	var CustomFormController = formController.extend({
        //--------------------------------------------------------------------------
        // Handlers
        //--------------------------------------------------------------------------

        /**
         * @private
         * @param {OdooEvent} ev
         */
        _onButtonClicked: function (ev) {
            // stop the event's propagation as a form controller might have other
            // form controllers in its descendants (e.g. in a FormViewDialog)
            ev.stopPropagation();
            var self = this;
            var def;

            this._disableButtons();

            function saveAndExecuteAction () {
                return self.saveRecord(self.handle, {
                    stayInEdit: true,
                }).then(function () {
                    // we need to reget the record to make sure we have changes made
                    // by the basic model, such as the new res_id, if the record is
                    // new.
                    var record = self.model.get(ev.data.record.id);
                    return self._callButtonAction(attrs, record);
                });
            }
            var attrs = ev.data.attrs;
            if (attrs.confirm) {
                def = new Promise(function (resolve, reject) {
                    Dialog.confirm(self, attrs.confirm, {
                        confirm_callback: saveAndExecuteAction,
                    }).on("closed", null, resolve);
                });
            } else if (attrs.special === 'cancel') {
                def = this._callButtonAction(attrs, ev.data.record);
            } else if (attrs.special == 'other') { // 新增自定义事件处理
                self._enableButtons(); // 启用按钮(点击后会自动禁用按钮)
                self.trigger_up('close_dialog'); // 关闭对话框
                return;
            } else if (!attrs.special || attrs.special === 'save') {
                // save the record but don't switch to readonly mode
                def = saveAndExecuteAction();
            } else {
                console.warn('Unhandled button event', ev);
                return;
            }

            // Kind of hack for FormViewDialog: button on footer should trigger the dialog closing
            // if the `close` attribute is set
            def.then(function () {
                self._enableButtons();
                if (attrs.close) {
                    self.trigger_up('close_dialog');
                }
            }).guardedCatch(this._enableButtons.bind(this));
        },
	});

odoo.__DEBUG__['services']['web.FormController'] = CustomFormController;
});

codePojects\odoo14\custom\estate\views\webclient_templates.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_common" inherit_id="web.assets_common" name="Backend Assets (used in backend interface)">
         <xpath expr="//script[last()]" position="after">
             <script type="text/javascript" src="/estate/static/src/js/views/form_controller.js"></script>
         </xpath>
    </template>
</odoo>

codePojects\odoo14\custom\estate\__manifest__.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
{
    'name': 'estate',
    'depends': ['base'],
    'data':[
        # ...略
        'views/webclient_templates.xml',
        'wizards/demo_wizard_views.xml',
        # ...略
    ]
}

方案2

研究发现,在不为按钮设置type属性的情况下,可以为按钮添加onclick属性,指定点击按钮时需要调用的javascript函数,不过,此时点击按钮,不会再调用web.FormController中定义的_onButtonClicked函数。示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <record id="demo_wizard_view_form" model="ir.ui.view">
            <field name="name">demo.wizard.form</field>
            <field name="model">demo.wizard</field>
            <field name="arch" type="xml">
                <form>
                    //...代码略 
                    <footer>
                        <button name="action_confirm" do_confirm_action('demo.wizard','action_confirm') string="确认" class="oe_highlight"/>
                        <button string="关闭" class="oe_link" special="cancel"/>
                    </footer>
                </form>
            </field>
        </record>
        //...代码略        
    </data>
</odoo>

codePojects\odoo14/estate/static/src/js/demo_wizard_views.js

function do_confirm_action(modelName, modelMethod){
    // do something
    //...
    $("button[name='action_confirm']").attr("disabled", true);
}

codePojects\odoo14\custom\estate\views\webclient_templates.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_common" inherit_id="web.assets_common" name="Backend Assets (used in backend interface)">
         <xpath expr="//script[last()]" position="after">
             <script type="text/javascript" src="/estate/static/src/js/demo_wizard_views.js"></script>
         </xpath>
    </template>
</odoo>

标签:wizard,自定义,form,views,self,按钮,attrs,Odoo
From: https://www.cnblogs.com/shouke/p/17094248.html

相关文章

  • RuntimePlatform.WindowsWebPlayer 或者 RuntimePlatform.OSXWebPlayer 弃用的,已过时
    问题:RuntimePlatform.WindowsWebPlayer或者RuntimePlatform.OSXWebPlayer弃用的,已过时解决方法在Unity2017和Unity2018中已过时,解决方法:returnApplication.plat......
  • webrtc 自定义对接摄像机视频流
    ​​https://blog.csdn.net/u013113491/article/details/80285181​​编码器伪装法​​https://blog.csdn.net/foruok/article/details/70237019​​众所周知浏览器不支持......
  • SpringBoot中自定义消息转化器
    场景1.SpringBoot自动配置了消息转化器。2.自定义消息转化器,只需要在类中添加消息转化器的@Bean,就会被SpringBoot自动加入到容器中。实现新建Controllerpackagecom.exampl......
  • 详解Spring AOP自定义可重复注解没有生效问题
    目录1.问题背景2.不啰嗦,上代码3.问题排查3.1是不是切点写得有问题,于是换成如下形式:3.2是不是使用的地方不是代理对象4.问题原因 1.问题背景工作中遇......
  • 自定义鼠标右键菜单
     鼠标右键弹出框<template><divclass="conversation-item-menubox-shadow1"><spanclass="menu-itemoperation-text"@click.stop="openNewPage">打......
  • abp vnext自定义claim
    创建UserClaimsPrincipalFactory工厂在Project.Domain中创建ProjectUserClaimsPrincipalFactoryusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;......
  • ABP的IdentityServer4中使用自定义的claim声明
    ABP的IdentityServer4使用自定义的claim声明,我是想增加一个部门Id,登录用户的中文名称在IdentityServer项目的AbpModule中,context.Services.AddScoped<IProfileServ......
  • abp 自定义token
    如何删除访问令牌中未使用的声明?ABP框架版本: v5.2.2用户界面类型:角度数据库提供者:EFCore分层(MVC)或身份服务器分离(角度):是/否异常消息和堆栈跟踪:重现问......
  • 一文搞懂工作流审批(Java+activiti)快速开发+自定义工作流配置
    前言activiti工作流引擎项目,企业erp、oa、hr、crm等企事业办公系统轻松落地,一套完整并且实际运用在多套项目中的案例,满足日常业务流程审批需求。一、项目形式springboot......
  • Hive使用TRANSFORM运行Python脚本总结
    1、Python环境设置可以使用addcachearchive的方法把tar.gz添加到分布式缓存,Hive会自动解压压缩包,但是目录名是和压缩包名称一样的;addcachearchive${env:my_workbenc......