Odoo 9.如何覆盖表单小部件? [英] Odoo 9. How to override form widgets?

查看:176
本文介绍了Odoo 9.如何覆盖表单小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用odoo 9 。在系统中存在 render_value 方法,用于每种类型的字段:

I work with odoo 9. In the system exists render_value method for each type of field:

/odoo/addons/web/static/src/js/views/form_widgets.js
/odoo/addons/web/static/src/js/views/form_relational_widgets.js

如何为所有表单使用我的自定义方法 render_value (例如在 FieldChar 中)?以及如何为一个表单或一个模块使用特定的 render_value

How I can use my custom method render_value(for example in FieldChar) for all forms? And how I can use specific render_value for one form or one module?

我在模块中创建了 form_widgets.js ,但我不明白如何正确覆盖Field。

I created form_widgets.js in my module, but I not understand how properly override Field.

odoo.define('my_module.form_widgets', function (require) {
"use strict";

// what I should do here???

});

你能提供一些小例子吗?提前谢谢。

Can you provide small example? Thank in advance.

推荐答案

我找到了解决方案。

您需要做的第一件事是为前端创建静态。 JS:

The first thing you need to do it is create static for frontend. JS:

// path_to_your_module/static/src/js/form_widgets.js
odoo.define('your_module.form_widgets', function (require) {
    "use strict";

    var core = require('web.core');
    var form_common = require('web.form_common');
    var FieldChar = core.form_widget_registry.get('char');

    FieldChar.include({
        // this is will be work for all FieldChar in the system
        template: 'MyChar', // my template for char fields
        // we can create here any logic for render
        //render_value: function() {
        //}
    });
    // this is widget for unique CharField
    var MyModuleFieldChar = FieldChar.extend({
        template: 'MyUniqueChar' // my custom template for unique char field
    });
    // register unique widget, because Odoo does not know anything about it
    core.form_widget_registry.add('my_unique_char', MyModuleFieldChar);

});

qWeb的模板

<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<!-- path_to_your_module/static/src/xml/form_widgets.xml -->
<t t-name="MyChar">
    <!-- for example I just added new <span> to all FieldChar -->
    <span>my_val</span>
    <!-- this is original content for CharField from path_to_odoo/addons/web/static/src/xml/base.xml -->
    <span t-att-class="'oe_form_field '+widget.widget_class" t-att-style="widget.node.attrs.style">
        <t t-if="!widget.get('effective_readonly')">
            <input t-att-type="widget.password ? 'password' : 'text'"
                t-att-barcode_events="widget.options.barcode_events"
                t-att-id="widget.id_for_label"
                t-att-tabindex="widget.node.attrs.tabindex"
                t-att-autofocus="widget.node.attrs.autofocus"
                t-att-placeholder="widget.node.attrs.placeholder"
                t-att-maxlength="widget.field.size"
            /><img class="oe_field_translate oe_input_icon" t-if="widget.field.translate" t-att-src='_s + "/web/static/src/img/icons/terp-translate.png"' width="16" height="16" border="0"/>
        </t>
        <t t-if="widget.get('effective_readonly')">
            <span class="oe_form_char_content"></span>
        </t>
    </span>
</t>
<!-- This is example template for my unique field -->
<t t-name="MyUniqueChar">
    <span>unique_char</span>
</t>

</templates>

第二步 - 包括我们的静态文件。

Second step - include our static files.

创建新视图,添加资产

<?xml version="1.0" encoding="utf-8"?>
<!-- path_to_your_module/views/assets.xml -->
<openerp>
    <data>
        <template id="assets_backend" name="mail assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/your_module/static/src/js/form_widgets.js"></script>
            </xpath>
        </template>
    </data>
</openerp>

在模块的 openerp .py 中添加下一部分:

In openerp.py of your module add next sections:

'data': [
    'views/assets.xml',
    # other files
],
'qweb': [
    'static/src/xml/*.xml',
],

此后,我们的FieldChar将对系统中的所有 CHAR 字段起作用。如果我们需要使用 my_unique_char ,我们只需要将属性小部件添加到我们的字段中,就像这样:

After this will be work our FieldChar for all CHAR fields in the system. If we need to use my_unique_char we need just add attribute widget to field of our from like this:

<field name="name" widget="my_unique_char"/>

我希望它会帮助别人。

这篇关于Odoo 9.如何覆盖表单小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆