Hi!
I want to know how to render a Many2one field like a kanban card, something similar to the 'partners' model 'child_ids' field in 'form' view.
I tried using the very same code but this widget only works in Many2many or One2many fields.
<field name="child_ids" mode="kanban" context="{'default_parent_id': active_id, 'default_street': street, 'default_street2': street2, 'default_city': city, 'default_state_id': state_id, 'default_zip': zip, 'default_country_id': country_id, 'default_supplier': supplier, 'default_customer': customer, 'default_lang': lang, 'default_user_id': user_id}"> <kanban> <field name="id"/> <field name="color"/> <field name="name"/> <field name="title"/> <field name="type"/> <field name="email"/> <field name="parent_id"/> <field name="is_company"/> <field name="function"/> <field name="phone"/> <field name="street"/> <field name="street2"/> <field name="zip"/> <field name="city"/> <field name="country_id"/> <field name="mobile"/> <field name="state_id"/> <field name="image_small"/> <field name="lang"/> <!-- fields in form x2many view to diminish requests --> <field name="comment"/> <field name="customer"/> <field name="display_name"/> <field name="supplier"/> <templates> <t t-name="kanban-box"> <t t-set="color" t-value="kanban_color(record.color.raw_value)"/> <div t-att-class="color + (record.title.raw_value == 1 ? ' oe_kanban_color_alert' : '') + ' oe_kanban_global_click'"> <div class="o_kanban_image"> <img alt="" t-if="record.image_small.raw_value" t-att-src="kanban_image('res.partner', 'image_small', record.id.raw_value)"/> <t t-if="!record.image_small.raw_value"> <img alt="Delivery" t-if="record.type.raw_value === 'delivery'" t-att-src="_s + "/base/static/img/truck.png""/> <img alt="Invoice" t-if="record.type.raw_value === 'invoice'" t-att-src="_s + "/base/static/img/money.png""/> <t t-if="record.type.raw_value !== 'invoice' && record.type.raw_value !== 'delivery'"> <img alt="Logo" t-if="record.is_company.raw_value === true" t-att-src="_s + "/base/static/img/company_image.png""/> <img alt="Avatar" t-if="record.is_company.raw_value === false" t-att-src="_s + "/base/static/img/avatar.png""/> </t> </t> </div> <div class="oe_kanban_details"> <field name="name"/> <div t-if="record.function.raw_value"><field name="function"/></div> <div t-if="record.email.raw_value"><field name="email" widget="email"/></div> <div t-if="record.type.raw_value != 'contact'"> <div> <field name="zip"/> <field name="city"/> </div> <field t-if="record.state_id.raw_value" name="state_id"/> <field name="country_id"/> </div> <div t-if="record.phone.raw_value">Phone: <field name="phone" widget="phone"/></div> <div t-if="record.mobile.raw_value">Mobile: <field name="mobile" widget="phone"/></div> </div> </div> </t> </templates> </kanban>
</field>
I also tried with a new widget extending from FieldMany2One
odoo.define('web_widget_many2one_template', function (require) {
"use strict";
var FieldMany2One = require('web.relational_fields').FieldMany2One;
var fieldRegistry = require('web.field_registry');
var core = require('web.core');
var qweb = core.qweb;
var rpc = require('web.rpc');
var webWidgetMany2oneTemplate = FieldMany2One.extend({
className: 'o_m2o_template',
supportedFieldTypes: ['many2one'],
init: function () {
this._super.apply(this, arguments);
this.rpc_rec = false;
this.readOnlyTemplate = 'many2one_template';
if ( this.nodeOptions.hasOwnProperty('template') && this.nodeOptions.template ) {
this.readOnlyTemplate = this.nodeOptions.template;
}
},
willStart: function () {
var self = this;
var self_qweb = qweb;
var def;
if ( this.hasOwnProperty('value') && this.value.hasOwnProperty('data') && this.value.data.hasOwnProperty('id')) {
var def = this._rpc({
model: this.value.model,
method: 'search_read',
domain: [
['id', '=', this.value.data.id]
],
}).then(function (result) {
if (Array.isArray(result) && result.length > 0) {
self.rpc_rec = result[0];
}
});
}
return $.when(this._super.apply(this, arguments), def);
},
_renderReadonly: function () {
var _this = this;
var self_qweb = qweb;
return $.when(this._super.apply(this, arguments)).then(function () {
var template_render = self_qweb.render(_this.readOnlyTemplate, {record: _this.rpc_rec});
_this.$el.empty().css({"width":"100%"}).append(template_render);
});
},
});
fieldRegistry.add('m2o_template', webWidgetMany2oneTemplate);
return webWidgetMany2oneTemplate;
});
But if the model has 40 many2one fields then there are 40 rpc calls
Thanks.