تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
6 الردود
15158 أدوات العرض

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
فبراير 20
2808
2
يناير 20
7215
2
ديسمبر 19
5465
0
يناير 25
1189
1
مارس 25
652