[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.
This commit is contained in:
Christophe Matthieu
2018-01-05 17:34:25 +01:00
committed by Martin Geubelle
parent b7ee1aa4b1
commit 73e2ba991c
3 changed files with 133 additions and 2 deletions
+17
View File
@@ -348,6 +348,23 @@ var utils = {
'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
].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
@@ -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);
},