diff options
| author | Christophe Matthieu <chm@odoo.com> | 2017-12-06 13:51:42 +0100 |
|---|---|---|
| committer | Martin Geubelle <mge@odoo.com> | 2018-01-05 17:34:25 +0100 |
| commit | 73e2ba991c0e74654f3c2e2ae02485196d25fe1e (patch) | |
| tree | 3243b16ee4c401b1d8ddf3998d23826be4ce60d1 /addons/web/static/src/js | |
| parent | b7ee1aa4b1ea906195c234020cb708a1da9de6a7 (diff) | |
[FIX] web: add a stableSort method for sorting x2m
Issue: create 60 lines in ordered x2many with the same value (0) for the order
field and make a change ; the displayed records will be switched.
The problem appears because the sorting function is not stable and the
records with the same values can change place (hence change page), which is
quite disturbing for the user.
Diffstat (limited to 'addons/web/static/src/js')
| -rw-r--r-- | addons/web/static/src/js/core/utils.js | 17 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/basic/basic_model.js | 5 |
2 files changed, 20 insertions, 2 deletions
diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js index ac9310dce90..acf8ca4d68d 100644 --- a/addons/web/static/src/js/core/utils.js +++ b/addons/web/static/src/js/core/utils.js @@ -349,6 +349,23 @@ var utils = { ].join(';'); }, /** + * Sort an array in place, keeping the initial order for identical values. + * + * @param {Array} array + * @param {function} iteratee + */ + stableSort: function (array, iteratee) { + var stable = array.slice(); + return array.sort(function stableCompare (a, b) { + var order = iteratee(a, b); + if (order !== 0) { + return order; + } else { + return stable.indexOf(a) - stable.indexOf(b); + } + }); + }, + /** * @param {any} array * @param {any} elem1 * @param {any} elem2 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 8a9fd496b39..78e9216be62 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -89,6 +89,7 @@ var Context = require('web.Context'); var core = require('web.core'); var Domain = require('web.Domain'); var session = require('web.session'); +var utils = require('web.utils'); var _t = core._t; @@ -3833,7 +3834,7 @@ var BasicModel = AbstractModel.extend({ for (var k = 0; k < list.orderedResIDs.length; k++) { orderedResIDs[list.orderedResIDs[k]] = k; } - list.res_ids.sort(function compareResIdIndexes (resId1, resId2) { + utils.stableSort(list.res_ids, function compareResIdIndexes (resId1, resId2) { if (!(resId1 in orderedResIDs) && !(resId2 in orderedResIDs)) { return 0; } @@ -3878,7 +3879,7 @@ var BasicModel = AbstractModel.extend({ } return compareRecords(record1ID, record2ID, level + 1); }; - list.res_ids.sort(compareRecords); + utils.stableSort(list.res_ids, compareRecords); } this._setDataInRange(list); }, |
