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

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>
形象
丢弃

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

最佳答案

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.

形象
丢弃
编写者

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

最佳答案

  <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. 

形象
丢弃
最佳答案

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" />

形象
丢弃
编写者 最佳答案

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



形象
丢弃
相关帖文 回复 查看 活动
0
2月 20
2804
2
1月 20
7201
2
12月 19
5455
0
1月 25
1167
1
3月 25
652