最近在做项目的时候遇到一个问题,其实之前也有遇到,就是说客户要求当一条记录的状态发生变化时,在指定状态的记录不可编辑,之前遇到这个问题是,所做的处理是保存的校验记录的状态,通过raise error的方式去阻止用户保存编辑,这种事后的处理客户不是很满意,经过一番研究,总算是从前端js的方向解决了这个问题。
核心代码:
1、 记录的模型动作
项目 my.project tree,form {'form_no_edit':[('state', '=', 'approval')]}
2、JS代码
odoo.define(‘my_module.hide_form_edit_btn’, function (require) {
“use strict”;
var FormView = require(‘web.FormView’);
FormView.include({
do_push_state: function (state) {
try {
var self = this;
this._super.apply(this, arguments);
var no_edit = this.options.action.context.form_no_edit
if(no_edit != undefined){
var result = this.compute_domain(no_edit);
if(resulttrue){
this.
b
u
t
t
o
n
s
.
f
i
n
d
(
"
.
o
f
o
r
m
b
u
t
t
o
n
s
v
i
e
w
"
)
.
h
i
d
e
(
)
;
t
h
i
s
.
buttons.find(".o_form_buttons_view").hide(); this.
buttons.find(".oformbuttonsview").hide();this.buttons.find(".o_dropdown_toggler_btn").hide()
}else{
if(this.get(“actual_mode”)“view”) {
this.$buttons.find(".o_form_buttons_view").show()
}
}
}
} catch (e) {
}
},
});
});
这个js全局有效,只要在context中获取到form_no_edit,并且form_no_edit=True,记录的表单视图的编辑按钮都会隐藏。