列表视图背景色
适用于13.0
有时候我们希望列表视图能够显示背景色来明显标记谋些记录,odoo原生不支持这种操作,因此我们在基础模块中增加了此功能的支持。下面来介绍一下具体的使用方法。
使用bg_color_field组件
我们在基础模块中定义了bg_color_field组件,我们需要在想要设置的列表视图中显示背景色的字段使用此组件。
<field name="name" widget="bg_color_field"/>
定义bg_color字段
定义了bg_color_field还不够,我们还需要一个辅助字段来告诉我们的字段需要什么颜色的背景色,因此还需要定义一个bg_color字段,通常这是一个计算字段。
<field name="bg_color" invisible="1"/>
根据业务逻辑返回bg_color的颜色
最后,我们根据自己的需求,在模型中根据不同的条件返回不同的bg_color值:
for order in self:
# 如果已经交货,显示无色
if order.delivery_state == 'shipped':
order.due_type = 'no'
order.bg_color = '#FFFFFF' # 无色
continue
commitment_date = order.commitment_date.date() if order.commitment_date else None
if commitment_date:
if commitment_date < fields.Date.today():
order.due_type = 'expired'
order.bg_color = '#FF0000' # 红色
elif commitment_date == fields.Date.today():
order.due_type = 'on_date'
order.bg_color = '#FFA500' # 橙色
else:
order.due_type = 'no'
order.bg_color = '#FFFFFF' # 无色
else:
order.due_type = 'no'
order.bg_color = '#FFFFFF' # 无色