From 3662c7246f90e8a7ea67aa52c71afd0cede330de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 1 Apr 2017 09:36:23 +0200 Subject: [PATCH] [FIX] web: unselect current row on one2many before doing something else In a one2many, when we click on 'Add an item', we need to unselect the current row. Otherwise, its changes will not be committed and it will be in practice discarded. --- .../static/src/js/views/basic/basic_model.js | 2 +- .../js/views/list/list_editable_renderer.js | 4 ++ .../test/fields/relational_fields_tests.js | 51 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 9139906f982..8f20f46b1cf 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -167,7 +167,7 @@ var BasicModel = AbstractModel.extend({ }; return this._makeDefaultRecord(list.model, params).then(function (id) { list.count++; - list._changes = list.data.slice(0); + list._changes = list._changes || list.data.slice(0); if (position === 'top') { list._changes.unshift(id); } else { diff --git a/addons/web/static/src/js/views/list/list_editable_renderer.js b/addons/web/static/src/js/views/list/list_editable_renderer.js index 2460e52cfea..1c1ea35acf2 100644 --- a/addons/web/static/src/js/views/list/list_editable_renderer.js +++ b/addons/web/static/src/js/views/list/list_editable_renderer.js @@ -485,10 +485,14 @@ ListRenderer.include({ _onAddRecord: function (event) { // we don't want the browser to navigate to a the # url event.preventDefault(); + // we don't want the click to cause other effects, such as unselecting // the row that we are creating, because it counts as a click on a tr event.stopPropagation(); + // but we do want to unselect current row + this._unselectRow(); + this.trigger_up('add_record'); }, /** diff --git a/addons/web/static/test/fields/relational_fields_tests.js b/addons/web/static/test/fields/relational_fields_tests.js index ac26d523b00..147eb8121de 100644 --- a/addons/web/static/test/fields/relational_fields_tests.js +++ b/addons/web/static/test/fields/relational_fields_tests.js @@ -1177,6 +1177,57 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('one2many list (editable): edition, part 2', function (assert) { + assert.expect(8); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + mockRPC: function (route, args) { + if (args.method === 'write') { + assert.strictEqual(args.args[1].p[0][0], 0, + "should send a 0 command for field p"); + assert.strictEqual(args.args[1].p[1][0], 0, + "should send a second 0 command for field p"); + } + return this._super.apply(this, arguments); + }, + }); + + // edit mode, then click on Add an item and enter a value + form.$buttons.find('.o_form_button_edit').click(); + form.$('.o_form_field_x2many_list_row_add a').click(); + form.$('td.o_edit_mode input').val('kartoffel').trigger('input'); + + // click again on Add an item + form.$('.o_form_field_x2many_list_row_add a').click(); + assert.strictEqual(form.$('td:contains(kartoffel)').length, 1, + "should have one td with the new value"); + assert.strictEqual(form.$('td.o_edit_mode input').length, 1, + "should have one other new td"); + assert.strictEqual(form.$('tr.o_data_row').length, 2, "should have 2 data rows"); + + // enter another value and save + form.$('td.o_edit_mode input').val('gemuse').trigger('input'); + form.$buttons.find('.o_form_button_save').click(); + assert.strictEqual(form.$('tr.o_data_row').length, 2, "should have 2 data rows"); + assert.strictEqual(form.$('td:contains(kartoffel)').length, 1, + "should have one td with the new value"); + assert.strictEqual(form.$('td:contains(gemuse)').length, 1, + "should have one td with the new value"); + + form.destroy(); + }); + QUnit.test('onchange in a one2many', function (assert) { assert.expect(1);