From 27ad94915682fd0e294636ca841c85fefd535704 Mon Sep 17 00:00:00 2001 From: Zarrar Date: Mon, 21 May 2018 12:53:06 +0530 Subject: [PATCH] [Fix] Sort data when added from quick entry in the list (#5575) * sort data in list use sort_by and sort_order individually instead of combining them into one order_by * patch to update user_settings - remove order_by and insert sort_by and sort_order in user_settings --- frappe/patches.txt | 1 + .../v11_0/update_list_user_settings.py | 26 +++++++++++++++++++ frappe/public/js/frappe/list/base_list.js | 8 ++++-- frappe/public/js/frappe/list/list_view.js | 26 +++++++++++++++++-- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 frappe/patches/v11_0/update_list_user_settings.py diff --git a/frappe/patches.txt b/frappe/patches.txt index e198bffc9f..f3c479fe5c 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -213,3 +213,4 @@ execute:frappe.delete_doc_if_exists('Page', 'user-permissions') frappe.patches.v10_0.set_no_copy_to_workflow_state frappe.patches.v10_0.increase_single_table_column_length frappe.patches.v11_0.create_contact_for_user +frappe.patches.v11_0.update_list_user_settings diff --git a/frappe/patches/v11_0/update_list_user_settings.py b/frappe/patches/v11_0/update_list_user_settings.py new file mode 100644 index 0000000000..5b3eb303f0 --- /dev/null +++ b/frappe/patches/v11_0/update_list_user_settings.py @@ -0,0 +1,26 @@ +from __future__ import unicode_literals +import frappe, json +from frappe.model.utils.user_settings import update_user_settings, sync_user_settings + +def execute(): + """ Update list_view's order by property from __UserSettings """ + + users = frappe.db.sql("select distinct(user) from `__UserSettings`", as_dict=True) + + for user in users: + # get user_settings for each user + settings = frappe.db.sql("select * from `__UserSettings` \ + where user='{0}'".format(frappe.db.escape(user.user)), as_dict=True) + + # traverse through each doctype's settings for a user + for d in settings: + data = json.loads(d['data']) + if data and ('List' in data) and ('order_by' in data['List']): + # convert order_by to sort_order & sort_by and delete order_by + order_by = data['List']['order_by'] + order_by = order_by.replace('`', '').split('.')[1] + data['List']['sort_by'], data['List']['sort_order'] = order_by.split(' ') + data['List'].pop('order_by') + update_user_settings(d['doctype'], json.dumps(data), for_update=True) + + sync_user_settings() diff --git a/frappe/public/js/frappe/list/base_list.js b/frappe/public/js/frappe/list/base_list.js index 2c86f067ed..a63daccb09 100644 --- a/frappe/public/js/frappe/list/base_list.js +++ b/frappe/public/js/frappe/list/base_list.js @@ -45,7 +45,8 @@ frappe.views.BaseList = class BaseList { this.fields = []; this.filters = []; - this.order_by = 'modified desc'; + this.sort_by = 'modified'; + this.sort_order = 'desc'; // Setup buttons this.primary_action = null; @@ -224,7 +225,10 @@ frappe.views.BaseList = class BaseList { this.sort_selector = new frappe.ui.SortSelector({ parent: this.filter_area.$filter_list_wrapper, doctype: this.doctype, - args: this.order_by, + args: { + sort_by: this.sort_by, + sort_order: this.sort_order + }, onchange: () => this.refresh(true) }); } diff --git a/frappe/public/js/frappe/list/list_view.js b/frappe/public/js/frappe/list/list_view.js index 5fc5675ca7..ef57e5ee3c 100644 --- a/frappe/public/js/frappe/list/list_view.js +++ b/frappe/public/js/frappe/list/list_view.js @@ -67,7 +67,8 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { this.filters = filters; } // initialize with saved order by - this.order_by = this.view_user_settings.order_by || 'modified desc'; + this.sort_by = this.view_user_settings.sort_by || 'modified'; + this.sort_order = this.view_user_settings.sort_order || 'desc'; // build menu items this.menu_items = this.menu_items.concat(this.get_menu_items()); @@ -127,6 +128,7 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { [this.meta.title_field, this.meta.image_field], (this.settings.add_fields || []), this.meta.track_seen ? '_seen' : null, + this.sort_by, 'enabled', 'disabled' ); @@ -304,7 +306,8 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { frappe.model.user_settings.save(this.doctype, 'last_view', this.view_name); this.save_view_user_settings({ filters: this.filter_area.get(), - order_by: this.sort_selector.get_sql_string() + sort_by: this.sort_selector.sort_by, + sort_order: this.sort_selector.sort_order }); } @@ -794,6 +797,25 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList { this.data[index] = datum; } + this.data.sort((a, b) => { + const a_value = a[this.sort_by] || ''; + const b_value = b[this.sort_by] || ''; + + let return_value = 0; + if (a_value > b_value) { + return_value = 1; + } + + if (b_value > a_value) { + return_value = -1; + } + + if (this.sort_order === 'desc') { + return_value = -return_value; + } + return return_value; + }); + this.render(); }); });