From 25548eb7e963632668438f939401403431201904 Mon Sep 17 00:00:00 2001 From: len-odoo Date: Fri, 11 May 2018 16:59:10 +0200 Subject: [FIX] web: sort relational fields by their names When sorting records according to a many2one field in a list view embedded in a form view, the js would sort according to their ids, in a seemingly arbitrary order. Thus sorting records according to a field with values in ("a", "b", "c") could be sorted as "a", "a", "c", "b" instead of "a", "a", "b", "c". We make the comparison on the name of the field instead. opw 1838860 --- addons/web/static/src/js/views/basic/basic_model.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'addons/web/static/src/js/views/basic/basic_model.js') 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 9e8afe1629f..d0eed4dc524 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -4100,10 +4100,20 @@ var BasicModel = AbstractModel.extend({ var r2 = self.localData[record2ID]; var data1 = _.extend({}, r1.data, r1._changes); var data2 = _.extend({}, r2.data, r2._changes); - if (data1[order.name] < data2[order.name]) { + + // Default value to sort against: the value of the field + var orderData1 = data1[order.name]; + var orderData2 = data2[order.name]; + + // If the field is a relation, sort on the display_name of those records + if (list.fields[order.name].type === 'many2one') { + orderData1 = orderData1 ? self.localData[orderData1].data.display_name : ""; + orderData2 = orderData2 ? self.localData[orderData2].data.display_name : ""; + } + if (orderData1 < orderData2) { return order.asc ? -1 : 1; } - if (data1[order.name] > data2[order.name]) { + if (orderData1 > orderData2) { return order.asc ? 1 : -1; } return compareRecords(record1ID, record2ID, level + 1); -- cgit v1.2.3