odoo开发笔记 -- odoo10 视图界面根据字段状态,动态隐藏创建&编辑按钮

场景描述:

 

解决方式:

网络搜索,vnsoft_form_hide_edit 找到了这个odoo8的模块,

odoo10语法和视图界面有新的变化,所以需要修改一些地方,感兴趣的小伙伴可以对比下两个代码的不同。

 

odoo10:

注意:该js代码是全局生效的。

odoo.define('vnsoft_form_hide_edit', function (require) {
    "use strict";
    var FormView = require('web.FormView');

    FormView.include({do_push_state: function (state) {
                // alert("hahahahahaha");
                try {
                    var self = this;
                    this._super.apply(this, arguments);
                    var no_edit = this.options.action.context.form_no_edit
                    // console.log("%o",no_edit);
                    if(no_edit!=undefined){
                        var result = this.compute_domain(no_edit);
                        // console.log("%o",result);
                        if(result==true){
                            this.$buttons.find(".o_form_buttons_view").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) {
                    { /* empty */ }
                  }
            }
        }
    );
});

关键部分:视图里边添加限制条件,来根据定义的条件,动态显示编辑按钮--显示or隐藏

        <record id="your_test_action" model="ir.actions.act_window">
            <field name="name">测试视图</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">teset.test_dec</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{ 'form_no_edit':['|',('state','in',['submitted','approved']),
                ('city', 'in', ['beijing','shanghai'])],}
            </field>
            <field name="view_id" ref="customs_declaration_tree"/>
        </record>

当form视图的相关字段: 字段(state状态)的值在“提交”或者“批准”时,

或者 字段(city城市)的值在“北京”或者”上海“的条件下,当前界面左上角的”编辑“按钮就会隐藏。

 

posted @ 2018-05-14 13:51  hello-Jesson  阅读(5413)  评论(1编辑  收藏  举报