Odoo Help

0

Custom form widget in one2many field doesn't render

By
Christoph
on 9/30/15, 3:20 AM 2,313 views

Problem

I created a custom form widget to render an icon to represent the states of an selection field. This field and widget is used in a one2many field/widget. The problem is now that it just works if you are editing the line of the one2many field. If you are just viewing the form without editing only the id of the selection field is shown as text.

View mode:

https://www.dropbox.com/s/ml7u08lz606bp85/one2many_customwidget_view.png?dl=0 

Edit mode:

https://www.dropbox.com/s/4ttq70lbnrbf8zi/one2many_customwidget_edit.png?dl=0 

(image embedding is currently not working for me)

Code

Java Script

    instance.web.form.widgets.add('managerstate', 'instance.web.form.FieldManagerState');

instance.web.form.FieldManagerState = instance.web.form.FieldChar.extend({
template: 'FieldManagerState',
widget_class: 'oe_form_field_managerstate',
render_value: function () {
if (this.get('value') == 'email_sent') {
this.$el.find('i').css('color', 'blue');
} else if (this.get('value') == 'activated') {
this.$el.find('i').css('color', 'red');
}
}
});

Qweb XML

<?xml version="1.0" encoding="UTF-8"?>
<templates>
<t t-name="FieldManagerState">
<span t-att-class="'oe_form_field '+widget.widget_class"
t-att-style="widget.node.attrs.style">
<i class="fa fa-eye" style="font-size: 1.5em; line-height: 28px; padding-left: 5px;"/>
</span>
</t>
</templates>

Do you have any idea how to render the widget in the view mode correctly (same as in the edit mode).

0
Christoph
On 10/21/15, 8:08 AM

I found the solution:

For the list view (i.e. also in x2many fields) you have to write an own widget renderer class. It is important which name the name/id after the add() function has. Here an extract of the comment in the code (you can find it in addons/web/static/src/js/view_list.js):

/**
* Registry for column objects used to format table cells (and some other tasks
* e.g. aggregation computations).
*
* Maps a field or button to a Column type via its ``$tag.$widget``,
* ``$tag.$type`` or its ``$tag`` (alone).
**/

Java Script

    instance.web.list.columns.add('field.managerstate', 'instance.web.list.Managerstate');

instance.web.list.Managerstate = instance.web.list.Column.extend({
_format: function (row_data, options) {
var value = row_data[this.id].value;
// do something
var style = 'red';
return QWeb.render('FieldManagerState.cell', {
'style': style,
});
}
});

Qweb XML

 <t t-name="FieldManagerState.cell><button type="button"><i class="fa fa-eye" t-att-style="style"/></button></t>

In this example I reduced the code to show how it works. It doesn't contains all the logic of the question. But as you maybe see you have to do the same logic as in the question (depends on how it works/looks like in the read, edit mode in the list or without list.

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 9/30/15, 3:20 AM
Seen: 2313 times
Last updated: 10/21/15, 8:11 AM