能否根据另一个字段的值动态更改按钮的动作?示例代码:
<xpath expr="//button[@class='oe_stat_button o_res_partner_tip_opp']" position="attributes">
<attribute name="name">%(action1)d</attribute>
<attribute name="name">%(action2)d</attribute>
</xpath>
该按钮的操作将是action1或action2,具体取决于布尔值/ select /任何字段的值。如何实现?
答案 0 :(得分:5)
至少有两种可能性:
创建多个按钮并按条件显示或隐藏它们
最后它应该看起来像:
<field name="my_selection_field" />
<button name="%(action1)d" string="Action 1" attrs="{'invisible': [('my_selection_field', '!=', 'selection1')]}" />
<button name="%(action2)d" string="Action 2" attrs="{'invisible': [('my_selection_field', '!=', 'selection2')]}" />
<button name="%(action3)d" string="Action 3" attrs="{'invisible': [('my_selection_field', '!=', 'selection3')]}" />
这显然不是完美的解决方案,但应该可以。
使用python方法返回操作
这也将起作用,但会更加动态。只需使按钮类型为object
,然后在name
属性中设置模型多记录方法即可。
<button action="button_dynamic_action" string="Action" type="object" />
现在在视图模型上实现该方法:
@api.multi
def button_dynamic_action(self):
self.ensure_one()
action = {}
if self.my_selection_field == 'selection1':
action = {
'name': _('Action 1'),
'view_type': 'form',
'view_mode': 'form',
'res_model': 'my.model',
#'view_id': # optional
'type': 'ir.actions.act_window',
#'res_id': # optional
'target': 'new' # or 'current'
}
elif self.my_selection_field == 'selection2':
action = {
'name': _('Action 2'),
'view_type': 'form',
'view_mode': 'tree',
'res_model': 'my.model',
#'view_id': # optional
'type': 'ir.actions.act_window',
#'res_id': # optional
'target': 'current' # or 'new'
}
# and so on
return action
您还可以从现有的窗口动作(ir.actions.act_window)中读取内容,而不是在代码中“创建”它们,以下示例来自Odoo本身:
res = self.env['ir.actions.act_window'].for_xml_id('base', 'action_attachment')
# ... change res with context or name and so on
return res