【问题标题】:Conditinal Formatting Odoo form view条件格式 Odoo 表单视图
【发布时间】:2019-03-01 06:30:36
【问题描述】:

我想实现以下场景。尝试了很多方法,但没有运气。

<t t-if="q1_percent &gt; 75">
    <td style="background-color:#52be80"><field name="q1_percent" nolabel="1"/></td>
</t>
<t t-elif="'q1_percent' &gt; 50 and 'q1_percent' &lt; 75">
    <td class="td_act" style="background-color:#f4d03f"><field name="q1_percent" nolabel="1"/></td>
</t>
<t t-elif="'q1_percent' &lt; 50">
    <td class="td_act" style="background-color:#e74c3c"><field name="q1_percent" nolabel="1"/></td>
</t>

我正在使用odoo 10。上面的代码是用于表单视图的。

我怎样才能做到这一点?任何想法任何帮助都非常感谢。谢谢!

【问题讨论】:

    标签: css xml forms odoo


    【解决方案1】:

    直到 Odoo 12 常规视图(树、表单等)和 QWeb 视图之间存在差异,这意味着常规视图不能与要评估的 QWeb 内容混合,以用于报告和网站页面。

    您仍然可以通过简单地定义一个计算的 HTML 字段来获取您正在寻找的内容,该字段将包含评估该 QWeb 代码的 HTML 结果,或者完全不使用 QWeb 直接构建 HTML。或者没有 QWeb 只是自己生成 HTML。

    例如:

    from lxml import etree
    
    q1_percent_html = fields.HTML("Q1 Percent HTML", compute='_compute_q1_percent_html')
    
    @api.depends('q1_percent')
    def _compute_q1_percent_html(self):
        for elem in self:
            # QWeb version
            t = etree.fromstring("""
                <div>
                    <t t-if="q1_percent &gt; 75">
                        <td style="background-color:#52be80"><t t-esc="q1_percent"/></td>
                    </t>
                    <t t-elif="'q1_percent' &gt; 50 and 'q1_percent' &lt; 75">
                        <td class="td_act" style="background-color:#f4d03f"><t t-esc="q1_percent"/></td>
                    </t>
                    <t t-elif="'q1_percent' &lt; 50">
                        <td class="td_act" style="background-color:#e74c3c"><t t-esc="q1_percent"/></td>
                    </t>
                <div>
            """)
            elem.q1_percent_html = self.env['ir.qweb'].render(t, {'q1_percent': elem.q1_percent})
    
            # Python direct version
            if elem.q1_percent >= 75:
                background_color = "#52be80"
            elif elem.q1_percent >= 50 and elem.q1_percent <= 75:
                background_color = "#f4d03f"
            elif elem.q1_percent <= 50:
                background_color = "#e74c3c"
            elem.q1_percent_html = """<div><td style="background-color:%s">%s</td></div>"""% (background_color, elem.q1_percent)
    

    在表单视图中使用该字段,例如:

    <field name="q1_percent_html" nolabel="1" readonly="1"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多