【问题标题】:Odoo XML Conditional Formatting TextOdoo XML 条件格式文本
【发布时间】:2019-11-19 01:01:27
【问题描述】:

我正在尝试使用工作室应用程序编辑 Odoo 上的 xml 代码。我想根据单元格的值设置文本颜色的格式 - 某些值是红色的,其他值是绿色的。

有人可以帮忙吗?

我使用上一个主题的答案作为指导。 XML Odoo field conditional color formatting

<field name="x_studio_mr_approval" string="MR Approval (test)" style="color: red;" attrs="{'invisible': ['|',['x_studio_mr_approval','=','Approved'],['x_studio_mr_approval','=','Approved Unseen']]}"/>

<field name="x_studio_mr_approval" string="MR Approval (test)" style="color: green;" attrs="{'invisible': ['|',['x_studio_mr_approval','=','New'],['x_studio_mr_approval','=','Awaiting Approval'],['x_studio_mr_approval','=','Rejected']]}"/>

我希望看到“已批准”和“未看到已批准”为绿色,而其他 3 个选项为红色。

我当前的代码会发生什么: - 如果值为“已批准”或“已批准未见”,则它可以正常工作并显示绿色字段。红色区域是不可见的。

  • 如果值为“Awaiting Approval”或“New”或“Rejected”,则无法正常工作。它在表单视图上显示字段和值两次。一个是红色的,另一个是绿色的。由于某种原因,它不会使绿色字段不可见

【问题讨论】:

    标签: xml text format conditional-statements odoo


    【解决方案1】:

    更好地使用域可能会有所帮助:

    <field name="x_studio_mr_approval" string="MR Approval (test)" style="color: red;" attrs="{'invisible': [('x_studio_mr_approval','in',['Approved','Approved Unseen'])]}"/>
    <field name="x_studio_mr_approval" string="MR Approval (test)" style="color: green;" attrs="{'invisible': ['!',('x_studio_mr_approval','in',['Approved','Approved Unseen'])]}"/>
    

    供参考,请阅读https://www.odoo.com/documentation/12.0/reference/orm.html#domains

    【讨论】:

    • 感谢您的帮助,该链接也很有用。您的代码给了我一个错误 - 它缺少一个括号,但我添加了这个并且它运行良好:) 'code''code'
    • 把同一个字段放两次是个坏主意,如果计算出来就没有问题,因为它总是在服务器端计算,但如果不是,odoo 总是取其中一个的值
    • 抱歉缺少括号。它已修复。
    【解决方案2】:

    另一种做法是使用类似的东西为这个特定字段生成一个小部件。

    <field name="x_studio_mr_approval" string="MR Approval (test)" widget="my_widget"/>
    

    在 js 中生成一个类似这样的小部件:

    odoo.define('widget_colored', function (require) {
        "use strict";
    
        var registry = require('web.field_registry');
        var basic_fields = require('web.basic_fields');
        var InputField = basic_fields.InputField;
    
        var FieldColored = InputField.extend({
            _render: function () {
                this.$el.html(this._formatValue(this.value));
                if (this.value === 'Approved') {
                    this.$el[0].className = this.$el[0].className + ' font-weight-bold'; // Using class
                }
                ...
            },
        });
    
        registry.add('my_widget', FieldColored);
    });
    

    不要忘记将 JS 集成到您的资产中

    <template id="assets_backend" inherit_id="web.assets_backend">
        <xpath expr=".">
           <script type="text/javascript" src="demo_erp_event/static/src/js/MYJS.js"/>
        </xpath>
    </template>
    

    【讨论】:

    • 感谢您的详细回复。这个解决方案比其他人建议的更复杂,但我感谢您为此所做的工作,我将牢记这些信息以备不时之需:)
    • @Alexandre,我也更喜欢你的回答。感谢分享。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多