Skip to Content
Menu
This question has been flagged
1 Reply
8986 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).

Avatar
Discard
Author Best Answer

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.

Avatar
Discard
Related Posts Replies Views Activity
0
Jun 21
3153
0
Aug 20
1939
0
Feb 18
2564
0
Sep 17
3204
0
Jul 16
3867