Help

1

how to view or hide field by attrs based on function (Automatically)?

Avatar
omar ahmed

i want to view or hide 'working_hours' field based on function ( Automatically without using button or anything)

@api.onchange('working_hours_view')
def hide_working_hours(self):
if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
'hr.group_hr_user'):
self.working_hours_view = True
else:
self.working_hours_view = False

working_hours_view = fields.Boolean(computed=hide_working_hours)
this is view:
<record id="hide_working_hours_for_employees" model="ir.ui.view">
<field name="name">Hide Working Hours Employees Form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='resource_calendar_id']" position="before">
<field name="working_hours_view" invisible="1"/>
</xpath>
<xpath expr="//field[@name='resource_calendar_id']" position="attributes">
<attribute name="attrs">{'invisible': [('working_hours_view' ,'=', False)]}</attribute>
</xpath>
      </field>
</record>
1 Comment
Avatar
Discard
Avatar
Sehrish
-

How to visible and invisible fields in odoo: https://goo.gl/BCxCpk

6 Answers
2

Your code seems correct. Just remove "api.onchange" decorator from your method and try it again.
System will automatically trigger the function without depending on any of the field.

1 Comment
Avatar
Discard
Avatar
omar ahmed
-

i deleted "api.onchange" decorator but still not working

1
Avatar
Jay Dasani
Best Answer

  <field name="working_hours"  attrs="{'invisible': [('field name ', '=', False)]}"/>

You can do this by using attrs field invisible now you just need to set condition field_name = False or field_name != False . Like this that working hours field will be hidden or will be displayed on based on condition.

Thanks. 

Avatar
Discard
0
Avatar
vivek
Best Answer

You Can hide a field or Button using below code , its work for me  and i'm using odoo version 9

here i'm hidding a button using which i'm checking a field(work_email) value is emty or not

my py file ( here i'm using inherited view of hr employee )

work_email = fields.Char('Work Email')
hide = fields.Boolean(string='Hide', compute="_compute_hide" ,store=False)

@api.depends('work_email')

def _compute_hide(self):

if not self.work_email:

self.hide = True

else:

self.hide = False

my xml file

<button class="oe_stat_button" name="send_personal_mail" string="Send Email"  type="object" icon="fa-envelope" attrs="{'invisible': [('hide','=', True)]}" /> 

<field name="hide" invisible="1" />

Avatar
Discard
0
Avatar
omar ahmed
Best Answer

i added the field before the function without depends or onchange and it works now automatically .. like this example:


class InheritHrEmployee(models.Model):
_inherit = 'hr.employee'

inv = fields.Boolean(string="Invisible", compute="c_inv", store=False)

@api.one
def c_inv(self):
if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
'hr.group_hr_user'):
self.inv = False
else:
self.inv = True



Avatar
Discard