實踐環境 Odoo 14.0-20221212 (Community Edition) 代碼實現 方案1 通過研究發現,點擊odoo form表單按鈕時,會調用odoo14\odoo\addons\web\static\src\js\views\form\form_controller.js文件中的 ...
實踐環境
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>
作者:授客
微信/QQ:1033553122
全國軟體測試QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限於時間倉促,文中可能存在錯誤,歡迎指正、評論!
作者五行缺錢,如果覺得文章對您有幫助,請掃描下邊的二維碼打賞作者,金額隨意,您的支持將是我繼續創作的源動力,打賞後如有任何疑問,請聯繫我!!!
微信打賞
支付寶打賞 全國軟體測試交流QQ群