通过js代码使字段不可见 - ODOO 9

时间:2016-12-20 13:54:23

标签: javascript widget odoo-9 invisible

我正在寻找一种让js上的字段不可见的方法(我正在制作一个自定义小部件' InvisibleIfEmptry')。 我在扩展_check_visibility类时尝试覆盖FormWidget.AbstractField方法:

var core = require('web.core'),
        form_common = require('web.form_common');
var InvisibleIfEmpty = form_common.AbstractField.extend({
start: function() {
            this.on("change:effective_readonly", this, function() {
                this._toggle_label();
                this._check_visibility();
            });
            this.render_value();
            this._toggle_label();
        },
_check_visibility: function() {
            if (this.get("effective_readonly"))
                this.$el.toggleClass('o_form_invisible',true);   
            }
                this.$el.toggleClass('o_form_invisible',false);
            }
        }, .....

但这只会使字段的值不可见,而不是标签。 我的猜测是改变一些field_manager的值,但我无法弄清楚哪一个?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

这是我执行此操作的JS代码:

odoo.define('myCustomModule', function(require)
{
    'use strict';
    var core = require('web.core'),
        form_common = require('web.form_common'),
        form_view = require('web.FormView');

    form_common.AbstractField.include({

        start: function() {

            this._super();
            // Check visibility logic below when content
            // changes or the form swich to view mode
            this.field_manager.on("view_content_has_changed", this, function() {
                this._check_visibility();
            });
            this.on("change:effective_readonly", this, function() {
                this._toggle_label();
                this._check_visibility();
        });
        },

        _check_visibility: function() {

            // If the form is in view mode and the field is empty,
            // make the field invisible
            window.alert(this.);
            if (this.field_manager.get("actual_mode") === "view" ) {
                if(this.get("value") == false){
                    this.$el.toggleClass('o_form_invisible',true);
                    this.$label.toggleClass('o_form_invisible',true);
                }else{
                    this.$el.toggleClass('o_form_invisible',this.get("effective_invisible"));
                    this.$label.toggleClass('o_form_invisible',this.get("effective_invisible"));
                }
            }else{
                this.$el.toggleClass('o_form_invisible',this.get("effective_invisible"));
                this.$label.toggleClass('o_form_invisible',this.get("effective_invisible"));
            }
        },
    });

});

但这适用于我的所有模块。 有人知道如何从AbstractField获取模块/模型名称吗?