From 4fd9db52a26701ca1ae028a8e625beaa0550ea62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 14 Mar 2018 10:09:39 +0100 Subject: [PATCH] [FIX] web: prevent issues with pager and x2m and limit When we are in the process of adding a record, we do not want to update the pager count. The reason is that if the user is at the limit (so, 1-40/40, no pager visible), then click on add, there will be a pager shown with 1-40/41. Now, imagine that the record currently being added as a required field, and the user clicks on the pager. In that case, we need to discard the newly added record (bringing the total back to 40), then move to next page, which does not exist anymore. So, to fix this issue, we simply consider that a record being added does not count, from the point of view of the pager. --- .../static/src/js/fields/relational_fields.js | 20 +++++- .../tests/fields/relational_fields_tests.js | 61 ++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index af3467d8c82..5856b59455a 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -928,6 +928,7 @@ var FieldX2Many = AbstractField.extend({ * did not agree to discard. */ _saveLine: function (recordID) { + var self = this; var def = $.Deferred(); var fieldNames = this.renderer.canBeSaved(recordID); if (fieldNames.length) { @@ -939,7 +940,9 @@ var FieldX2Many = AbstractField.extend({ } else { this.renderer.setRowMode(recordID, 'readonly').done(def.resolve.bind(def)); } - return def; + return def.then(function () { + self.pager.updateState({ size: self.value.count }); + }); }, /** * Parses the 'columnInvisibleFields' attribute to search for the domains @@ -1177,7 +1180,20 @@ var FieldOne2Many = FieldX2Many.extend({ return this._super.apply(this, arguments).then(function () { if (ev && ev.target === self && ev.data.changes && self.view.arch.tag === 'tree') { if (ev.data.changes[self.name].operation === 'CREATE') { - var index = self.editable === 'top' ? 0 : self.value.data.length - 1; + var index = 0; + if (self.editable !== 'top') { + index = self.value.data.length - 1; + // we consider that a new record, created in the bottom, + // does not count as a record worth mentioning in the + // pager, at least not until the line has been saved. + // We prevent the pager from increasing its size, which + // means that the pager is not displayed when we just + // reach the limit. For example, if limit is 3, if we + // have 3 records, and we click on add, we will see the + // 4 records on the same page, but we do not want a + // pager. + self.pager.updateState({ size: self.value.count - 1}); + } var newID = self.value.data[index].id; self.renderer.editRecord(newID); } diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index dfafb58abd5..5891c5d1515 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -2077,7 +2077,7 @@ QUnit.module('relational_fields', { data: this.data, arch: '
' + '' + - '' + + '' + '' + '' + '' + @@ -3994,6 +3994,37 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('editable one2many list, pager is updated', function (assert) { + assert.expect(1); + + this.data.turtle.records.push({id:4, turtle_foo: 'stephen hawking'}); + this.data.partner.records[0].turtles = [1,2,3,4]; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + }); + + // add a record, then click in form view to confirm it + form.$buttons.find('.o_form_button_edit').click(); + form.$('.o_field_x2many_list_row_add a').click(); + form.$el.click(); + + assert.strictEqual(form.$('.o_cp_pager').text().trim(), '1-3 / 5', + "pager should display the correct total"); + form.destroy(); + }); + + QUnit.test('one2many list (non editable): edition', function (assert) { assert.expect(12); @@ -4302,6 +4333,34 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('editable one2many list, adding line when only one page', function (assert) { + assert.expect(1); + + this.data.partner.records[0].turtles=[1,2,3]; + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + }); + + // add a record, to reach the page size limit + form.$buttons.find('.o_form_button_edit').click(); + form.$('.o_field_x2many_list_row_add a').click(); + + // the record currently being added should not count in the pager + assert.ok(!form.$('.o_cp_pager').is(':visible'), + "pager should not be visible"); + form.destroy(); + }); + QUnit.test('unselecting a line with missing required data', function (assert) { assert.expect(5);