From 2cf5e8fb62f5ec88d13ad613c76f65fab8b50d65 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 4 Mar 2021 15:11:38 +0000 Subject: [FIX] web: correctly edit Many2ManyCheckboxes with 100+ values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Many2ManyCheckboxes widget displays all values that could be in the many2many relation, with a checkbox indicating whether each value is in the relation or not. It is designed to be set on fields where the comodel contains a few records (typically, we don't want to see dozens of checkboxes in the form view). This widget shouldn't be used on many2manys with a large comodel, as we have better tools to handle them (like a tree view). We deal with extreme cases (when the widget is, by mistake, set on a field where the comodel is huge) by using the name_search limit of 100: at most 100 checkboxes are displayed. Before this commit, this extreme situation wasn't correctly handled. If there were in the relation records that weren't displayed (because they weren't inside the 100 limit), then, editing the value by (un)selecting a checkbox would automatically remove all non displayed values from the relation. This commit ensures that we keep in the relation all values that aren't displayed. Issue spotted when working on opw~2439041 closes odoo/odoo#67373 X-original-commit: fa324ef071952d749f635f878bee0fa462cfa011 Signed-off-by: Géry Debongnie (ged) Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/js/fields/relational_fields.js | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'addons/web/static/src/js/fields') diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 9efd76985cc..0ad11dba91a 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2762,9 +2762,20 @@ var FieldMany2ManyCheckBoxes = AbstractField.extend({ * @private */ _onChange: function () { + // Get the list of selected ids var ids = _.map(this.$('input:checked'), function (input) { return $(input).data("record-id"); }); + // The number of displayed checkboxes is limited to 100 (name_search + // limit, server-side), to prevent extreme cases where thousands of + // records are fetched/displayed. If not all values are displayed, it may + // happen that some values that are in the relation aren't available in the + // widget. In this case, when the user (un)selects a value, we don't + // want to remove those non displayed values from the relation. For that + // reason, we manually add those values to the list of ids. + const displayedIds = this.m2mValues.map(v => v[0]); + const idsInRelation = this.value.res_ids; + ids = ids.concat(idsInRelation.filter(a => !displayedIds.includes(a))); this._setValue({ operation: 'REPLACE_WITH', ids: ids, -- cgit v1.2.3