翼度科技»论坛 编程开发 python 查看内容

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

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
实践环境

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
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <odoo>
  3.     <data>
  4.         <record id="demo_wizard_view_form" model="ir.ui.view">
  5.             <field name="name">demo.wizard.form</field>
  6.             <field name="model">demo.wizard</field>
  7.             <field name="arch" type="xml">
  8.                 <form>
  9.                     //...代码略
  10.                     <footer>
  11.                         <button name="action_confirm" special="other" type="object" string="确认" />
  12.                         <button string="关闭"  special="cancel"/>
  13.                     </footer>
  14.                 </form>
  15.             </field>
  16.         </record>
  17.         //...代码略        
  18.     </data>
  19. </odoo>
复制代码
重定义web.FormController以实现重写_onButtonClicked
codePojects\odoo14/estate/static/src/js/views/form_controller.js
  1. odoo.define('customModule.FormController', function (require) {
  2.     "use strict";
  3.         var formController = require('web.FormController');
  4.         var CustomFormController = formController.extend({
  5.         //--------------------------------------------------------------------------
  6.         // Handlers
  7.         //--------------------------------------------------------------------------
  8.         /**
  9.          * @private
  10.          * @param {OdooEvent} ev
  11.          */
  12.         _onButtonClicked: function (ev) {
  13.             // stop the event's propagation as a form controller might have other
  14.             // form controllers in its descendants (e.g. in a FormViewDialog)
  15.             ev.stopPropagation();
  16.             var self = this;
  17.             var def;
  18.             this._disableButtons();
  19.             function saveAndExecuteAction () {
  20.                 return self.saveRecord(self.handle, {
  21.                     stayInEdit: true,
  22.                 }).then(function () {
  23.                     // we need to reget the record to make sure we have changes made
  24.                     // by the basic model, such as the new res_id, if the record is
  25.                     // new.
  26.                     var record = self.model.get(ev.data.record.id);
  27.                     return self._callButtonAction(attrs, record);
  28.                 });
  29.             }
  30.             var attrs = ev.data.attrs;
  31.             if (attrs.confirm) {
  32.                 def = new Promise(function (resolve, reject) {
  33.                     Dialog.confirm(self, attrs.confirm, {
  34.                         confirm_callback: saveAndExecuteAction,
  35.                     }).on("closed", null, resolve);
  36.                 });
  37.             } else if (attrs.special === 'cancel') {
  38.                 def = this._callButtonAction(attrs, ev.data.record);
  39.             } else if (attrs.special == 'other') { // 新增自定义事件处理
  40.                 self._enableButtons(); // 启用按钮(点击后会自动禁用按钮)
  41.                 self.trigger_up('close_dialog'); // 关闭对话框
  42.                 return;
  43.             } else if (!attrs.special || attrs.special === 'save') {
  44.                 // save the record but don't switch to readonly mode
  45.                 def = saveAndExecuteAction();
  46.             } else {
  47.                 console.warn('Unhandled button event', ev);
  48.                 return;
  49.             }
  50.             // Kind of hack for FormViewDialog: button on footer should trigger the dialog closing
  51.             // if the `close` attribute is set
  52.             def.then(function () {
  53.                 self._enableButtons();
  54.                 if (attrs.close) {
  55.                     self.trigger_up('close_dialog');
  56.                 }
  57.             }).guardedCatch(this._enableButtons.bind(this));
  58.         },
  59.         });
  60. odoo.__DEBUG__['services']['web.FormController'] = CustomFormController;
  61. });
复制代码
codePojects\odoo14\custom\estate\views\webclient_templates.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <odoo>
  3.     <template id="assets_common" inherit_id="web.assets_common" name="Backend Assets (used in backend interface)">
  4.          <xpath expr="//script[last()]" position="after">
  5.             
  6.          </xpath>
  7.     </template>
  8. </odoo>
复制代码
codePojects\odoo14\custom\estate\__manifest__.py
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. {
  4.     'name': 'estate',
  5.     'depends': ['base'],
  6.     'data':[
  7.         # ...略
  8.         'views/webclient_templates.xml',
  9.         'wizards/demo_wizard_views.xml',
  10.         # ...略
  11.     ]
  12. }
复制代码
方案2

研究发现,在不为按钮设置type属性的情况下,可以为按钮添加onclick属性,指定点击按钮时需要调用的javascript函数,不过,此时点击按钮,不会再调用web.FormController中定义的_onButtonClicked函数。示例如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <odoo>
  3.     <data>
  4.         <record id="demo_wizard_view_form" model="ir.ui.view">
  5.             <field name="name">demo.wizard.form</field>
  6.             <field name="model">demo.wizard</field>
  7.             <field name="arch" type="xml">
  8.                 <form>
  9.                     //...代码略
  10.                     <footer>
  11.                         <button name="action_confirm" special="other" type="object" string="确认" />
  12.                         <button string="关闭"  special="cancel"/>
  13.                     </footer>
  14.                 </form>
  15.             </field>
  16.         </record>
  17.         //...代码略        
  18.     </data>
  19. </odoo>
复制代码
codePojects\odoo14/estate/static/src/js/demo_wizard_views.js
  1. function do_confirm_action(modelName, modelMethod){
  2.     // do something
  3.     //...
  4.     $("button[name='action_confirm']").attr("disabled", true);
  5. }
复制代码
codePojects\odoo14\custom\estate\views\webclient_templates.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <odoo>
  3.     <template id="assets_common" inherit_id="web.assets_common" name="Backend Assets (used in backend interface)">
  4.          <xpath expr="//script[last()]" position="after">
  5.             
  6.          </xpath>
  7.     </template>
  8. </odoo>
复制代码
来源:https://www.cnblogs.com/shouke/p/17094248.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具