From 9f49709ef3ea8a522baf1e0e1e0ca7061921636a Mon Sep 17 00:00:00 2001 From: Carlos Mayo Date: Wed, 13 Nov 2019 09:56:04 +0000 Subject: [PATCH] [FIX] web: many2many_checkboxes: react to domain changes Let's assume a many2many_checkboxes widget in a form view with a dynamic domain (depending on another field in the view). At first rendering, the widget contains a checkbox for each value matching the domain. Before this rev., if the user changed the value of the field used in the domain, the many2many_checkboxes wasn't redrawn, so it still displayed the values matching the previous version of the domain. Closes #38509 Closes #40173 closes odoo/odoo#42867 X-original-commit: 1ace56f9a8cb56fb39235468dd13447bcbbee40a Signed-off-by: Aaron Bohy (aab) Co-authored-by: Aaron Bohy --- .../static/src/js/fields/relational_fields.js | 14 +++++++-- .../tests/fields/relational_fields_tests.js | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 0a3bb640f50..c1f116784fa 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2571,17 +2571,27 @@ var FieldMany2ManyCheckBoxes = AbstractField.extend({ /** * @private */ - _render: function () { + _renderCheckboxes: function () { var self = this; - this._super.apply(this, arguments); + this.m2mValues = this.record.specialData[this.name]; + this.$el.html(qweb.render(this.template, {widget: this})); _.each(this.value.res_ids, function (id) { self.$('input[data-record-id="' + id + '"]').prop('checked', true); }); }, /** + * @override + * @private + */ + _renderEdit: function () { + this._renderCheckboxes(); + }, + /** + * @override * @private */ _renderReadonly: function () { + this._renderCheckboxes(); this.$("input").prop("disabled", true); }, diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index f8c466099e0..c3f9a95ea7d 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -2314,6 +2314,35 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('widget many2many_checkboxes: values are updated when domain changes', async function (assert) { + assert.expect(5); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: `
+ + + `, + res_id: 1, + viewOptions: { + mode: 'edit', + }, + }); + + assert.strictEqual(form.$('.o_field_widget[name=int_field]').val(), '10'); + assert.containsN(form, '.o_field_widget[name=timmy] .custom-checkbox', 2); + assert.strictEqual(form.$('.o_field_widget[name=timmy] .o_form_label').text(), 'goldsilver'); + + await testUtils.fields.editInput(form.$('.o_field_widget[name=int_field]'), 13); + + assert.containsOnce(form, '.o_field_widget[name=timmy] .custom-checkbox'); + assert.strictEqual(form.$('.o_field_widget[name=timmy] .o_form_label').text(), 'silver'); + + form.destroy(); + }); + QUnit.module('FieldMany2ManyBinaryMultiFiles'); QUnit.test('widget many2many_binary', async function (assert) {