Odoo 16全局状态栏切换添加确认弹窗功能实现求助
Odoo 16 全局状态栏切换确认弹窗实现方案
问题根源
你的代码无效是因为错误地patch了FormController的_onFieldChanged方法:
- 这个方法是在字段值已经完成变更后才触发,无法拦截切换动作
- 状态栏(FieldStatus)的阶段切换逻辑由自身组件独立处理,不会触发FormController的
_onFieldChanged事件
正确的做法是直接patch负责渲染状态栏的FieldStatus组件,在阶段点击的源头添加确认逻辑。
完整解决方案
1. 替换JS代码(static/src/js/stage_confirmation.js)
/** @odoo-module **/ import { patch } from "@web/core/utils/patch"; import { FieldStatus } from "@web/views/fields/statusbar/statusbar_field"; import { Dialog } from "@web/core/dialog/dialog"; import { useService } from "@web/core/utils/hooks"; patch(FieldStatus.prototype, 'custom_stage_confirmation', { setup() { super.setup(); this.dialog = useService("dialog"); }, async _onStageClick(ev) { // 阻止默认的阶段切换动作 ev.preventDefault(); ev.stopPropagation(); const stageId = ev.currentTarget.dataset.stageId; const stageName = ev.currentTarget.dataset.stageName; // 弹出确认对话框 const confirmed = await this.dialog.confirm({ title: '确认切换阶段', body: `确定要将状态切换到「${stageName}」吗?`, confirmLabel: '确认', cancelLabel: '取消', }); if (confirmed) { // 用户确认后,执行原本的阶段切换逻辑 await super._onStageClick(ev); } }, });
2. 确认manifest.py配置(无需修改,补全必要参数)
你的配置基本正确,补全可安装参数确保模块正常部署:
# -*- coding: utf-8 -*- { 'name': "custom_stage_confirmation", 'author': "Marín", 'category': 'Uncategorized', 'version': '0.1', 'depends': ['web'], 'assets': { 'web.assets_backend': [ 'custom_stage_confirmation/static/src/js/stage_confirmation.js', ], }, 'installable': True, 'auto_install': False, }
3. 模块生效步骤
- 在Odoo后台应用列表找到
custom_stage_confirmation,点击升级 - 强制刷新浏览器缓存(Ctrl+Shift+R),确保新JS代码加载
关键说明
- 拦截时机:在
_onStageClick方法开头阻止默认动作,先弹出确认框,用户确认后才执行原逻辑 - 全局生效:因为patch的是全局的
FieldStatus组件,所有使用状态栏的模型(任务、销售订单、工单等)都会自动应用该确认逻辑 - UI一致性:使用Odoo内置的
dialog服务弹出标准模态框,保持系统界面风格统一
内容的提问来源于stack exchange,提问作者Andrés Marín
相关产品推荐
相关产品推荐

