From 1f37e4e0d67a6f9c36502b2ade2fa81eba0ead28 Mon Sep 17 00:00:00 2001 From: "Nasreddin Boulif (bon)" Date: Sun, 8 Aug 2021 19:49:07 +0000 Subject: [PATCH] [FIX] web: Allow array value for many2many search filter Issue: In an action, if we add to the context a key `search_default_x_ids` (who is a many2many field) with an array of ids as value, it will display/use only the first value in the search bar. Cause: If filter-type is 'field' and it's an array, it will take the first value. Solution: Take first value of array only if field-type is a `many2one`. Inspired by Odoo v13.0 opw-2596345 closes odoo/odoo#74936 X-original-commit: 1083bd204a10c9ec1708ba09e85d50fcba8ba8af Signed-off-by: Julien Mougenot (jum) Signed-off-by: bon-odoo --- .../control_panel_model_extension.js | 2 +- .../control_panel_model_extension_tests.js | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js b/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js index aba25a83849..da93ad344b7 100644 --- a/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js +++ b/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js @@ -858,7 +858,7 @@ odoo.define("web/static/src/js/control_panel/control_panel_model_extension.js", child.attrs.isDefault = true; let value = this.searchDefaults[child.attrs.name]; if (child.tag === 'field') { - child.attrs.defaultValue = Array.isArray(value) ? value[0] : value; + child.attrs.defaultValue = this.fields[child.attrs.name].type === 'many2one' && Array.isArray(value) ? value[0] : value; } else if (child.tag === 'groupBy') { child.attrs.defaultRank = typeof value === 'number' ? value : 100; } diff --git a/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js b/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js index 199c4b64baa..487414b678e 100644 --- a/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js +++ b/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js @@ -369,5 +369,52 @@ odoo.define("web/static/tests/control_panel/control_panel_model_extension_tests. }); + QUnit.test('search defaults on X2M fields', async function (assert) { + assert.expect(1); + + const context = { + search_default_otom: [1, 2], + search_default_mtom: [1, 2] + }; + const fields = this.fields; + fields.otom = { string: "O2M", type: "one2many", relation: 'partner' }; + fields.mtom = { string: "M2M", type: "many2many", relation: 'partner' }; + const arch = ` + + + + `; + const model = createModel({ arch, fields, context }); + assert.deepEqual(sanitizeFilters(model), [ + { + "defaultAutocompleteValue": { + "label": [1, 2], + "operator": "ilike", + "value": [1, 2] + }, + "defaultRank": -10, + "description": "O2M", + "fieldName": "otom", + "fieldType": "one2many", + "isDefault": true, + "type": "field" + }, + { + "defaultAutocompleteValue": { + "label": [1, 2], + "operator": "ilike", + "value": [1, 2] + }, + "defaultRank": -10, + "description": "M2M", + "fieldName": "mtom", + "fieldType": "many2many", + "isDefault": true, + "type": "field" + } + ]); + + }); + }); });