From 405beaa3c6cde0c365530c415aea9ce5ff764497 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 14 Oct 2011 10:43:10 +0200 Subject: [PATCH] [FIX] m2o values display in list views (mostly under onchange condition) Normal m2o values are pairs of (id, name) (name_get format, basically) but in some cases (lazyness & al), APIs (often onchange results) only return the `id` part, which is not sufficient to display the m2o as-is, a name_get has to be performed. Added an intermediate step in displaying a table cell (in list view): if we're displaying an m2o cell (~a cell for an m2o column) and the value is a number, then dispatch a name_get and keep trucking (display the rows without the m2o values). When the name_get returns, it will simply refresh the row and diplay the m2o name. bzr revid: xmo@openerp.com-20111014084310-pv2mmmy28fwwk9sm --- addons/web/static/src/js/view_list.js | 24 ++++++++++++++++++++++-- addons/web/static/src/xml/base.xml | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/js/view_list.js b/addons/web/static/src/js/view_list.js index 0ea1f7ec944..e62dcdc2fd9 100644 --- a/addons/web/static/src/js/view_list.js +++ b/addons/web/static/src/js/view_list.js @@ -785,6 +785,26 @@ openerp.web.ListView.List = openerp.web.Class.extend( /** @lends openerp.web.Lis [this.records.at(this.dataset.index).get('id'), this.dataset]); }, + render_cell: function (record, column) { + if (column.type === 'many2one') { + var value = record.get(column.id); + // m2o values are usually name_get formatted, [Number, String] + // pairs, but in some cases only the id is provided. In these + // cases, we need to perform a name_get call to fetch the actual + // displayable value + if (typeof value === 'number' || value instanceof Number) { + // fetch the name, set it on the record (in the right field) + // and let the various registered events handle refreshing the + // row + new openerp.web.DataSet(this.view, column.relation) + .name_get([value], function (names) { + if (!names.length) { return; } + record.set(column.id, names[0]); + }); + } + } + return openerp.web.format_cell(record.toForm().data, column); + }, render: function () { if (this.$current) { this.$current.remove(); @@ -792,7 +812,7 @@ openerp.web.ListView.List = openerp.web.Class.extend( /** @lends openerp.web.Lis this.$current = this.$_element.clone(true); this.$current.empty().append( QWeb.render('ListView.rows', _.extend({ - render_cell: openerp.web.format_cell}, this))); + render_cell: $.proxy(this, 'render_cell')}, this))); this.pad_table_to(5); }, pad_table_to: function (count) { @@ -900,7 +920,7 @@ openerp.web.ListView.List = openerp.web.Class.extend( /** @lends openerp.web.Lis record: record, row_parity: (index % 2 === 0) ? 'even' : 'odd', view: this.view, - render_cell: openerp.web.format_cell + render_cell: $.proxy(this, 'render_cell') }); }, /** diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index 8dbd3401b4c..3f955789b11 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -636,7 +636,7 @@ - +