Javascript Odoo:JSON:Inherit视图,有条件地隐藏编辑按钮
我试图在Odoo中隐藏BOM表表单中的“编辑”按钮,具体取决于布尔值的状态 我成功地删除了编辑按钮,代码如下:Javascript Odoo:JSON:Inherit视图,有条件地隐藏编辑按钮,javascript,json,xml,xpath,openerp,Javascript,Json,Xml,Xpath,Openerp,我试图在Odoo中隐藏BOM表表单中的“编辑”按钮,具体取决于布尔值的状态 我成功地删除了编辑按钮,代码如下: <xpath expr="//form[@string='Bill of Material']" position="attributes"> <attribute name="edit">false</attribute> </xpath> 这也给了我错误 为什么会出现此错误?如何修复此错误?这只是一个语法错误还是还有其他
<xpath expr="//form[@string='Bill of Material']" position="attributes">
<attribute name="edit">false</attribute>
</xpath>
这也给了我错误
为什么会出现此错误?如何修复此错误?这只是一个语法错误还是还有其他问题?我通过使用field\u view\u get成功地解决了一个类似的问题,但前提是将“realman”传递到上下文中
def fields\u view\u get(self、cr、uid、view\u id=None、view\u type='form',context=None、toolbar=False、submenu=False):
res=models.Model.fields\u view\u get(self、cr、uid、view\u id=view\u id、view\u type=view\u type、context=context、toolbar=toolbar、submenu=submenu)
realman=context.get('realman',True)
如果不是realman和view_type==“form”:
doc=etree.XML(res['arch'])
对于doc.xpath中的t(//form[@string='Bill of Material']):
t、 属性['edit']='false'
res['arch']=etree.tostring(doc)
返回res
如果realman是一个字段,您需要启用/禁用编辑按钮,那么恐怕这是不可能的。AFAIK.如前所述,他的解决方案不适用于取决于模型字段的按钮/动作。我已经用Javascript编写了解决方案。它在奥多10中工作(应该也在9中,但我还没有测试过)
示例是检查模型的字段“state”。如果它的值为“noEditable”,则编辑和删除按钮将隐藏。仅覆盖启用了“操作”是不够的,因为在尚未加载数据记录的情况下,ODOO正在调用该方法。因此,在方法显示并重新加载之后,需要再次检查它
// modify default form view for custom model my.custom.model
odoo.define('my.custom_model_form', function (require) {
"use strict";
var FormView = require('web.FormView');
FormView.include({
is_action_enabled: function(action) {
if (this.model == "my.custom.model" && this.datarecord && this.datarecord.state == "noEditable" &&
(action == 'delete' || action == 'edit')) {
// don't allow edit nor delete
return false;
}
// call default is_action_enabled method
return this._super.apply(this, arguments);
},
deleteItem: null,
deleteItemIdx: null,
deleteItemShown: true,
reinit_actions: function() {
// apply for my custom model only
if (this.model == "my.custom.model") {
// hide/show edit button
if (this.is_action_enabled('edit')) {
this.$buttons.find(".o_form_button_edit").show();
} else {
this.$buttons.find(".o_form_button_edit").hide();
}
// find delete item in sidebar's items
if (!this.deleteItem) {
// form view is adding it to "other"
if (this.sidebar && this.sidebar.items && this.sidebar.items.other) {
for (var i = 0; i < this.sidebar.items.other.length; i++) {
// on_button_delete is used as callback for delete button
// it's ugly way to find out which one is delete button, haven't found better way
if (this.sidebar.items.other[i].callback == this.on_button_delete) {
this.deleteItem = this.sidebar.items.other[i];
this.deleteItemIdx = i;
break;
}
}
}
}
// hide/show delete button
if (this.is_action_enabled('delete')) {
if (!this.deleteItemShown) {
this.deleteItemShown = true;
// add delete item to sidebar's items
this.sidebar.items.other.splice(this.deleteItemIdx, 0, this.deleteItem);
}
} else
if (this.deleteItemShown) {
this.deleteItemShown = false;
// remove delete item from sidebar's items
this.sidebar.items.other.splice(this.deleteItemIdx, 1);
}
}
},
reload: function() {
var self = this;
// run reinit_actions after reload finish
return this._super.apply(this, arguments).done(function() {
self.reinit_actions();
});
},
do_show: function() {
var self = this;
// run reinit_actions after do_show finish
return this._super.apply(this, arguments).done(function() {
self.reinit_actions();
});
}
});
});
//修改自定义模型my.custom.model的默认表单视图
odoo.define('my.custom\u model\u form',函数(require){
“严格使用”;
var FormView=require('web.FormView');
FormView.include({
是否启用了动作:功能(动作){
if(this.model==“my.custom.model”&&this.datarecord&&this.datarecord.state==“noEditable”&&
(操作=='删除'| |操作=='编辑'){
//不允许编辑或删除
返回false;
}
//调用默认值为\u操作\u启用的方法
返回此参数。\u super.apply(此参数为参数);
},
deleteItem:null,
deleteItemIdx:null,
结果显示:正确,
reinit_操作:函数(){
//仅适用于我的自定义模型
if(this.model==“my.custom.model”){
//隐藏/显示编辑按钮
如果(此操作已启用(“编辑”)){
这是。$buttons.find(“.o_form_button_edit”).show();
}否则{
这是。$buttons.find(“.o_form_button_edit”).hide();
}
//在侧边栏的项目中查找删除项目
如果(!this.deleteItem){
//表单视图正在将其添加到“其他”
if(this.sidebar&&this.sidebar.items&&this.sidebar.items.other){
for(var i=0;i
我相信使用计算字段的解决方案会使这些情况变得更简单
您可以继承BOM表并替代它。将不可见条件指向要在其中创建验证的计算字段。请注意,将计算字段添加到表单是必要的,即使将其隐藏
<xpath expr="//form[@string='Bill of Material']" position="attributes">
<attribute name="attrs">
{'invisible': [('show_bm_button', '=', False)]}
</attribute>
<field name="show_bm_button" invisible="1"/>
</xpath>
请记住从lxml导入etree!
<xpath expr="//form[@string='Bill of Material']" position="attributes">
<attribute name="attrs">
{'invisible': [('show_bm_button', '=', False)]}
</attribute>
<field name="show_bm_button" invisible="1"/>
</xpath>
class ProductTemplate(models.Model):
_inherit = 'product.template'
show_bm_button = fields.Boolean(
compute='_compute_show_bm_button',
readonly=False, store=False
)
# @api.depends('realman') #Just as a sample, if you have this field in this Model
def _compute_show_bm_button(self):
for record in self:
realman = self._context.get('realman') or True
if realman:
record.show_bm_button = True
else:
record.show_bm_button = False