Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to use custom widgets in tree views ?

By
Romain
on 4/3/14, 12:23 PM 5,627 views

I would like to store a period of time in minutes and display it to the user formatted like [hours]h[minutes] in OpenERP 7. Later I would like to allow users to modify the period in this format, so a field function isn't enough. I choose to create a custom widget that will do the formatting and later add a JavaScript TimePicker.

It works fine in form views but not in tree views, the init() of the widget is never called.

I configured the widget as follow:

myview.xml :

<!-- (...) -->
<field name="the_time" widget="hour"/>
<!-- (...) -->

static/js/xml/mymodule.xml :

openerp.mymodule  = function(instance) {

    instance.mymodule.FieldHour = instance.web.form.AbstractField.extend({

        init: function() {
            this._super.apply(this, arguments);
            this.set("value", "");
        },

        render_value: function() {
            val       = this.get("value");
            minutes   = val % 60;
            hours     = Math.round((val - minutes) / 60);

            if (minutes <= 9) {
                minutes = "0" + minutes;
            }

            if (hours <= 9) {
                hours = "0" + hours;
            }

            this.$el.text(hours + "h" +  minutes);
        },

    });

    instance.web.form.widgets.add('hour', 'instance.mymodule.FieldHour');
};

static/src/xml/mymodule.xml :

<templates id="template" xml:space="preserve">
    <t t-name="FieldHour">
        <div class="oe_field_hour">
            <t t-if="! widget.get('effective_readonly')">
                <input type="text"></input>
            </t>
        </div>
    </t>
</templates>

It seems related to OpenERP Web Client Bug #666427 but it's a quite old bug. I would be surprised if anyone else ran into this problem and found a workaround.

1

Leonardo Donelli

--Leonardo Donelli--
1040
| 5 4 8
Cave, Italy
--Leonardo Donelli--

Physics student and part-time developer.

Leonardo Donelli
On 6/18/14, 4:41 AM

Your custom widget is a form view custom widget: you extend web.form.AbstractField which is defined in web/addons/web/static/src/js/view_form.js and defines components used in form views. in the same directory you'll find view_list.js, view_list_editable.js and view_tree.js.

In particular in view_list.js you'll find "column definitions" for various type of fields, you could try working on those to recreate a tree view equivalent of your custom form field widget.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/3/14, 12:23 PM
Seen: 5627 times
Last updated: 3/22/16, 12:49 AM