Odoo Smart Button(动态按钮)

Odoo系统中主要包含下面三类按钮, 见下图:

Odoo Smart Button

也有人将Odoo的Smart Button直译为“智能按钮”的,感觉稍显夸张。另外,布尔按钮实际上也是一种特殊的动态按钮。

动态按钮有什么好处?

Table 1. 放在一起比较一下:

普通按钮

动态按钮

odoo regular button
odoo smart button1
  • 首先最显然的就是好看啦,当然好不好看是各花入各眼对的事,所以其样式是支持自定义的

  • 除了如普通按钮点击可触发绑定的按钮动作以外,它还可以动态的显示相关的数据信息,这也是之所以被称之为动态按钮的原因。

如何创建Odoo动态按钮

Table 2. 详解动态按钮

简单转换

single odoo regular button
single odoo plain smart button
<button class="oe_inline" type="action"
string="Opportunity"
name="..." context="..."/>
<button class="oe_inline oe_stat_button" type="action"
string="Opportunity"
name="..." context="..."/>

加个图标

图标字体-Font Awesome: http://fortawesome.github.io/Font-Awesome/

single smart button with icon
<button class="oe_inline" type="action"
string="Opportunity"
icon="fa-star"
name="..." context="..."/>

button标签内可以添加任意HTML

single smart button html
<button class="..." type="..." name="..." context="...">
    <p> Hello <strong>Odoo</strong></p>
</button>

添加Html+field

single smart button with field
<button class="..." type="..." name="..." context="..." icon="fa-star">
    <span><field name="opportunity_count"/> Opportunities<span>
</button>

动态按钮的自定义

动态按钮的自定义,分为两步:

动态内容的自定义

通过创建函数字段来定义动态数据,比如客户所对应的发票金额:

    total_invoiced = fields.Monetary(compute='_invoice_total', string="Total Invoiced",
        groups='account.group_account_invoice')

    @api.multi
    def _invoice_total(self):
        account_invoice_report = self.env['account.invoice.report']
        ...
        return result

视图控件的自定义

比如添加下面的button定义到视图,其中使用了最常用的statinfo控件

statinfo 控件

<button type="action" class="oe_stat_butto"
        icon="fa-pencil-square-o" name="..." context="...">
    <field name="total_invoiced" widget="statinfo"/>
</button>
Odoo Smart Button with infostat widget

另外还可以使用的控件,比如:

percentpie 控件

<button type="action" class="oe_stat_butto">
    <field name="received_ratio"
    string="Received"
    widget="percentpie"/>
</button>
Odoo Smart Button with percentpie widget

barchart 控件

<button type="action" class="oe_stat_butto">
    <field name="opened_daily"
    string="Opened Daily"
    widget="barchart"/>
</button>
Odoo Smart Button with barchart widget