This question has been flagged
1 Reply
17612 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.

Avatar
Discard
Best Answer

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.

Avatar
Discard