Skip to Content
Menu
This question has been flagged
3 Replies
7125 Views

Hello,

I'm trying to make a custom widget that makes fields invisible on "effective_readonly" if they're empty, and visible on "edit_mode".

Here is my try :

mymodule.js file

odoo.define('mymodule', function(require){
    var core = require('web.core'),       
    form_common = require('web.form_common');

    var InvisibleIfEmpty = form_common.AbstractField.extend({    
            init: function(field_manager, node)
            {           
                this._super(field_manager, node);
                this.set({'value' : ""});
            },       
            renderElement: function () {            
                if (this.get("effective_readonly")) {
                     if(this.get("value") != ""){                   
                          this.set({ 'effective_invisible' : true});
                }               
                     else{                                   
                          this.set({ 'effective_invisible' : false});
                }
                }else {
                    this.set({ 'effective_invisible' : false});
            }           
            this._super();
        },
    });
    core.form_widget_registry.add(
            'invisibleIfEmpty', InvisibleIfEmpty);
    return {  InvisibleIfEmpty: InvisibleIfEmpty,}
});

And mymodule.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <template id="assets_backend" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script src="/mymodule/static/src/js/mymodule.js" type="text/javascript" />
        </xpath>   
</template>
</odoo>

But this obviously does'nt work !

I'll appreciate any help on this :)

Thank you.






Avatar
Discard

It's more easy than your code. Let me answer you properly as soon as I can. But my answer will be based on what it's done on the module web_widget_color if you wanna see it first

Author

@Axel Mendoza : Thank you for the reply.

I gave a look to web_widget_color module's code, but it doesn't seem to be what i'm looking for ..., i'm surely missing something i guess.

I tried to override _check_visibility function and altering "this.$el.toggleClass('o_form_invisible',true);" but it only makes the value invisible, not the label :/

Author Best Answer

So, I found a solution that works but it's not a widget and applies to almost all fields. The idea is to override _check_visibility method for the main Field model ( AbstractField from form_common.js).

here is the js code :

odoo.define('mymodule', function(require){
    var core = require('web.core'),
        form_common = require('web.form_common');

        form_common.AbstractField.include({
            start: function() {
            this.field_manager.on("view_content_has_changed", this, function() {
                this._toggle_label();
                this._check_visibility();
            });
            this._super();
        },
        _check_visibility: function() {
            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"));
            }       
},   
});
});


this works for me :)

Avatar
Discard
Related Posts Replies Views Activity
0
Sep 23
498
0
Sep 23
451
2
Feb 23
9254
6
Oct 23
19286
0
Apr 22
1646