From 5164d30c922e3d2eba6db5764ee6035a64629d00 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Sat, 30 Sep 2017 15:55:39 +0200 Subject: [PATCH] [FIX] web: correctly redraw x2m list after onchange When an onchange is set on an x2many field displayed as an editable list in a form view, and the user updates one of the subrecords, the updated subrecord is kept in the DOM and updated, and all other subrecords in the relation are re-rendered and re-inserted at their correct place in the list (because the onchange might have changed them). Before this rev., the order of the subrecords after the edited one was reversed. --- .../js/views/list/list_editable_renderer.js | 4 +- .../tests/fields/relational_fields_tests.js | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) 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 3f59c879e1c..8f0e73cb466 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 @@ -160,6 +160,7 @@ ListRenderer.include({ } }); var newRowIndex = _.findIndex(state.data, {id: id}); + var $lastRow = $row; _.each(state.data, function (record, index) { if (index === newRowIndex) { return; @@ -168,7 +169,8 @@ ListRenderer.include({ if (index < newRowIndex) { $newRow.insertBefore($row); } else { - $newRow.insertAfter($row); + $newRow.insertAfter($lastRow); + $lastRow = $newRow; } }); if (self.currentRow !== null) { diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index a96e21cc443..bb7dc5db00e 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -3689,6 +3689,44 @@ QUnit.module('relational_fields', { AbstractField.prototype.destroy = oldDestroy; }); + QUnit.test('one2many editable list with onchange keeps the order', function (assert) { + assert.expect(2); + + this.data.partner.records[0].p = [1, 2, 4]; + this.data.partner.onchanges = { + p: function () {}, + }; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'
' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + viewOptions: { + mode: 'edit', + }, + }); + + assert.strictEqual(form.$('.o_data_cell').text(), 'first recordsecond recordaaa', + "records should be display in the correct order"); + + form.$('.o_data_row:first .o_data_cell').click(); + form.$('.o_selected_row .o_field_widget[name=display_name]').val('new').trigger('input'); + form.$el.click(); // click outside to validate the row + + assert.strictEqual(form.$('.o_data_cell').text(), 'newsecond recordaaa', + "records should be display in the correct order"); + + form.destroy(); + }); + QUnit.test('one2many list (editable): readonly domain is evaluated', function (assert) { assert.expect(2);