qweb的格式化options - xiaohao0576/odoo-doc GitHub Wiki

浮点数格式化

保留两位小数

<span t-out="doc.amount_total" t-options="{'widget': 'float', 'precision': 2}"/>

最多保留三位小数,整数时没有小数

<span t-out="('%.3f' % (line.price_unit or 0.0)).rstrip('0').rstrip('.')"/>

去掉小数后的0和小数点,并且设置为人类容易阅读的方式

<span t-field="o.amount_total" t-options='{"hide_trailing_zeros":true,"human_readable":true}'/>

日期格式化

<t t-out="doc.date_order" t-options='{"widget": "date", "format": "YYYY-MM-dd"}'/>

打印当前时间

<span t-out="context_timestamp(datetime.datetime.utcnow()).strftime('%Y-%m-%d %H:%M:%S')"/>

在每页放上页码

<div name="pager" t-if="report_type == 'pdf'">
    Page: <span class="page"/> / <span class="topage"/>
</div>

通过服务器动作返回报表url action

# 使用生成器表达式将每个整数转换为字符串,然后用逗号连接
ids = ",".join(map(str, records.ids))

# 定义 URL
url = 'https://your-domain.odoo.com/report/html/report.name/' + ids

# 创建 URL 动作
action = {
    'type': 'ir.actions.act_url',
    'url': url,
    'target': 'new'  # 在新的标签页中打开
}

表格中的文字保留空格或换行

        <table>
        <t t-set="index" t-value="0"/>
        <t t-foreach="doc.order_line" t-as="line">
            <t t-set="index" t-value="index + 1"/>
            <tr>
                <td style="width: 32px; color:white;">
                     <span style="white-space: pre;">    <t t-out="index"/></span>
                </td>
                <td style="width: 310px;">
                   <span style="white-space: pre-line;"> <t t-out="line.name"/></span>
                </td> 
                <td style="width: 56px;">
                   <span style="white-space: pre;"> <t t-out="line.product_uom_qty" t-options="{'widget': 'float', 'precision': 2}"/></span>
                </td>
                <td style="width: 65px;">
                    <span style="white-space: pre;"> <t t-out="line.product_uom.name"/></span>
                </td>
                <td style="width: 80px;">
                    <span style="white-space: pre;"> <t t-out="line.price_unit" t-options="{'widget': 'float', 'precision': 2}"/></span>
                </td>
                <td style="width: auto;">
                   <span style="white-space: pre;">  <t t-out="line.price_subtotal" t-options="{'widget': 'float', 'precision': 2}"/></span>
                </td>
            </tr>
        </t>
        </table>
⚠️ **GitHub.com Fallback** ⚠️