跳至内容
菜单
此问题已终结
2 回复
1244 查看

I am trying to hide a field based on an option selected from a drop down.

In my py file I have:


hide = fields.Boolean(string= 'Hide' , compute= "_compute_hide" )

@api.onchange( 'math_type_cfg_id' )

def _onchange_math_type_cfg_id(self):

    if self .math_type_cfg_id == '5' :

         self.hide = True

    else :

         self.hide = False


In my view I have:


<field name = "c_math_cfg_id"                                         

                                         required = "math_type_cfg_id == 5"

                                         invisible = "'[('hide', '=', True)]"                                       

                                         

                                         />



The required works but the field will not disappear.

Any help would be greatly appreciated!

形象
丢弃
最佳答案

Hello, 

try :

//.py

    math_type_cfg_id = fields.Selection([

        ('1', 'Type 1'),

        ('2', 'Type 2'),

        ('3', 'Type 3'),

        ('4', 'Type 4'),

        ('5', 'Type 5'),

    ], string="Math Type")


    hide = fields.Boolean(string='Hide', compute="_compute_hide")


    @api.depends('math_type_cfg_id')

    def _compute_hide(self):

        for record in self:

            record.hide = record.math_type_cfg_id == '5'


// .xml

<field name="c_math_cfg_id"

       required="math_type_cfg_id == '5'"

       invisible="[['hide', '=', True]]"

/>


Thanks

形象
丢弃
编写者 最佳答案

I managed to resolve it.

Invisible needs to be before required and then I didn't need the on change function.


Can't close or delete the question as I do not have enough karma

形象
丢弃
相关帖文 回复 查看 活动
4
5月 25
2705
2
5月 25
6137
1
3月 25
1811
4
3月 25
4703
3
2月 25
5774