From deed2ac28c71998ab147a9e70e1f9c032a524cf1 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 9 Aug 2011 10:56:28 +0200 Subject: [PATCH] [FIX] incorrect traversal of the tree: field handling the parent-child relationship *must not* be hardcoded (it's provided by fields_view_get) Also parent records provide the ids of their children directly, and fvg gives the name of the field in which these children live, there is no need to perform a search + read for every level of the tree past the first one, just perform a read() of the provided ids bzr revid: xmo@openerp.com-20110809085628-klvic2fmlvfmxjt3 --- addons/base/static/src/js/tree.js | 113 +++++++++++++++++----------- addons/base/static/src/xml/base.xml | 10 +-- 2 files changed, 74 insertions(+), 49 deletions(-) diff --git a/addons/base/static/src/js/tree.js b/addons/base/static/src/js/tree.js index dcdcfa65d8a..acb588366a1 100644 --- a/addons/base/static/src/js/tree.js +++ b/addons/base/static/src/js/tree.js @@ -39,9 +39,27 @@ openerp.base.TreeView = openerp.base.View.extend({ toolbar: this.view_manager ? !!this.view_manager.sidebar : false }, this.on_loaded); }, - + /** + * Returns the list of fields needed to correctly read objects. + * + * Gathers the names of all fields in fields_view_get, and adds the + * field_parent (children_field in the tree view) if it's not already one + * of the fields to fetch + * + * @returns {Array} an array of fields which can be provided to DataSet.read_slice and others + */ + fields_list: function () { + var fields = _.keys(this.fields); + if (!_(fields).contains(this.children_field)) { + fields.push(this.children_field); + } + return fields; + }, on_loaded: function (fields_view) { var self = this; + // field name in OpenERP is kinda stupid: this is the field holding + // the ids to the children of the current node + this.children_field = fields_view['field_parent']; this.fields_view = fields_view; _(this.fields_view.arch.children).each(function (field) { if (field.attrs.modifiers) { @@ -50,43 +68,50 @@ openerp.base.TreeView = openerp.base.View.extend({ }); this.fields = fields_view.fields; - this.dataset.read_slice(_.keys(this.fields), 0, false, function (response) { + this.dataset.read_slice(this.fields_list(), 0, false, function (records) { self.$element.html(QWeb.render('TreeView', { - "first_level": response, 'title': self.fields_view.arch.attrs.string })); - - self.$element.find('#parent_id').bind('change', function(){ - self.getdata($('#parent_id').val(), false); + var $select = self.$element.find('select') + .change(function () { + var $option = $(this).find(':selected'); + self.getdata($option.val(), $option.data('children')); + }); + _(records).each(function (record) { + $('