From 11f1ecd8df2850650621aaf856a70965c466dee7 Mon Sep 17 00:00:00 2001 From: Xavier Codinas Date: Mon, 8 Aug 2016 17:08:16 +0200 Subject: [PATCH] Add support for column sorting issue5675 review31301002 --- CHANGELOG | 1 + src/model.js | 9 ++++++++ src/sao.less | 3 +++ src/screen.js | 13 ++++++++---- src/view/tree.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4b1b823b95..c9ca59bb40 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add support for column sorting * Manage confirm attribute on buttons * Call autocompletion when setting record value * Limit readonly state for xxx2Many diff --git a/src/model.js b/src/model.js index 20aff19e8a..6b914e8508 100644 --- a/src/model.js +++ b/src/model.js @@ -437,6 +437,15 @@ }; return jQuery.when().then(browse_child); }; + array.sort = function(ids) { + var id2record = {}; + this.forEach(function(record) { + id2record[record.id] = record; + }); + ids.forEach(function(ordered_record, i){ + this[i] = id2record[ordered_record.id]; + }.bind(this)); + }; return array; }; diff --git a/src/sao.less b/src/sao.less index 9bdc47cbb1..11f6b90877 100644 --- a/src/sao.less +++ b/src/sao.less @@ -62,6 +62,9 @@ button { width: 30px; } & > thead > tr > th { + & > label.sortable { + cursor: pointer; + } overflow: hidden; text-overflow: ellipsis; min-width: 30px; diff --git a/src/screen.js b/src/screen.js index e9fa83adf1..5fca96e7fa 100644 --- a/src/screen.js +++ b/src/screen.js @@ -627,6 +627,7 @@ this.size_limit = null; this.limit = attributes.limit || Sao.config.limit; this.offset = 0; + this.order = attributes.order; var access = Sao.common.MODELACCESS.get(model_name); if (!(access.write || access.create)) { this.attributes.readonly = true; @@ -778,7 +779,8 @@ }.bind(this); return _switch(); }, - search_filter: function(search_string) { + search_filter: function(search_string, only_ids) { + only_ids = only_ids || false; if (this.context_screen) { if (this.context_screen_prm.state() == 'pending') { return this.context_screen_prm.then(function() { @@ -827,9 +829,12 @@ } var grp_prm = this.model.find(domain, this.offset, this.limit, - this.attributes.order, this.context); - var count_prm = this.model.execute('search_count', [domain], - this.context); + this.order, this.context); + var count_prm = jQuery.when(this.search_count); + if (!only_ids) { + count_prm = this.model.execute('search_count', [domain], + this.context); + } count_prm.done(function(count) { this.search_count = count; }.bind(this)); diff --git a/src/view/tree.js b/src/view/tree.js index 9bd39742fa..62b4267982 100644 --- a/src/view/tree.js +++ b/src/view/tree.js @@ -100,6 +100,13 @@ label.addClass('editable'); } } + if (column.sortable) { + var arrow = jQuery(''); + label.append(arrow); + column.arrow = arrow; + th.click(column, this.sort_model.bind(this)); + label.addClass('sortable'); + } tr.append(th.append(label)); column.header = th; }, this); @@ -191,7 +198,11 @@ affix_attributes)); } } - + if (!this.attributes.sequence && + !this.children_field && + model.fields[name].description.sortable !== false){ + column.sortable = true; + } this.fields[name] = true; // TODO sum } else if (child.tagName == 'button') { @@ -201,6 +212,47 @@ this.columns.push(column); }.bind(this)); }, + sort_model: function(e){ + var column = e.data; + var arrow = column.arrow; + var arrow_top = 'glyphicon glyphicon-triangle-top'; + var arrow_bottom = 'glyphicon glyphicon-triangle-bottom'; + this.columns.forEach(function(col) { + if (col.arrow){ + if (col != column && col.arrow.hasClass('glyphicon')) { + col.arrow.removeClass(arrow_top + ' ' + arrow_bottom); + } + } + }); + this.screen.order = null; + if (arrow.hasClass(arrow_bottom)) { + arrow.removeClass(arrow_bottom); + arrow.addClass(arrow_top); + this.screen.order = [[column.attributes.name, 'DESC']]; + } else if (arrow.hasClass(arrow_top)) { + arrow.removeClass(arrow_top); + } else { + arrow.addClass(arrow_bottom); + this.screen.order = [[column.attributes.name, 'ASC']]; + } + var unsaved_records = []; + this.screen.group.forEach(function(unsaved_record) { + if (unsaved_record.id < 0) { + unsaved_records = unsaved_record.group; + } + }); + var search_string = this.screen.screen_container.get_text(); + if ((!jQuery.isEmptyObject(unsaved_records)) || + (this.screen.search_count == this.screen.group.length) || + (this.screen.parent)) { + this.screen.search_filter(search_string, true).then( + function(ids) { + this.screen.group.sort(ids); + }.bind(this)); + } else { + this.screen.search_filter(search_string); + } + }, get_buttons: function() { var buttons = []; this.columns.forEach(function(column) {