[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
This commit is contained in:
Zarrar 2018-05-21 12:53:06 +05:30 committed by Faris Ansari
parent 467604ba93
commit 27ad949156
4 changed files with 57 additions and 4 deletions

View file

@ -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

View file

@ -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()

View file

@ -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)
});
}

View file

@ -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();
});
});