fix(report): save group by settings in report

This commit is contained in:
Rushabh Mehta 2019-05-06 20:01:33 +05:30
parent 07746ff7c9
commit 3cab2186f3
2 changed files with 68 additions and 27 deletions

View file

@ -32,30 +32,16 @@ frappe.ui.GroupBy = class {
//Set aggregate on options as numeric fields if function is sum or average
this.aggregate_function_select.on('change', () => {
this.report_view.meta.fields.forEach((field) => {
let fn = this.aggregate_function_select.val();
if(fn === 'sum' || fn === 'avg') {
// pick numeric fields for sum / avg
if(frappe.model.is_numeric_field(field.fieldtype)) {
this.aggregate_on_select.append(
$('<option>', { value : field.fieldname })
.text(field.label));
}
this.aggregate_on_select.show();
} else {
// count, so no aggregate function
this.aggregate_on_select.hide();
}
});
this.show_hide_aggregate_on();
});
// try running on change
this.groupby_select.on('change', () => this.apply_group_by());
this.aggregate_function_select.on('change', () => this.apply_group_by());
this.aggregate_on_select.on('change', () => this.apply_group_by());
this.groupby_select.on('change', () => this.apply_group_by_and_refresh());
this.aggregate_function_select.on('change', () => this.apply_group_by_and_refresh());
this.aggregate_on_select.on('change', () => this.apply_group_by_and_refresh());
$('.set-groupby-and-run').on('click', () => {
this.apply_group_by();
this.apply_group_by_and_refresh();
});
$('.remove-groupby').on('click', () => {
@ -63,6 +49,46 @@ frappe.ui.GroupBy = class {
});
}
show_hide_aggregate_on() {
this.report_view.meta.fields.forEach((field) => {
let fn = this.aggregate_function_select.val();
if(fn === 'sum' || fn === 'avg') {
// pick numeric fields for sum / avg
if(frappe.model.is_numeric_field(field.fieldtype)) {
this.aggregate_on_select.append(
$('<option>', { value : field.fieldname })
.text(field.label));
}
this.aggregate_on_select.show();
} else {
// count, so no aggregate function
this.aggregate_on_select.hide();
}
});
}
get_settings() {
if (this.group_by) {
return {
group_by: this.group_by,
aggregate_function: this.aggregate_function,
aggregate_on: this.aggregate_on
};
} else {
return null;
}
}
apply_settings(settings) {
this.groupby_select.val(settings.group_by);
this.aggregate_function_select.val(settings.aggregate_function);
this.show_hide_aggregate_on();
this.aggregate_on_select.val(settings.aggregate_on);
this.groupby_edit_area.show();
this.apply_group_by();
}
make_group_by_button() {
let group_by_button = $(`<div class="tag-groupby-area">
<div class="active-tag-groupby">
@ -76,8 +102,8 @@ frappe.ui.GroupBy = class {
}
apply_group_by() {
this.group_by = this.page.wrapper.find('.groupby option:selected').val();
this.aggregate_function = this.page.wrapper.find('.aggregate-function option:selected').val();
this.group_by = this.groupby_select.val();
this.aggregate_function = this.aggregate_function_select.val();
if (this.aggregate_function === 'count') {
this.aggregate_on = 'name';
@ -101,6 +127,10 @@ frappe.ui.GroupBy = class {
//If function is count add a new field for count
this.page.wrapper.find('.set-groupby-and-run').hide();
}
apply_group_by_and_refresh() {
this.apply_group_by();
this.report_view.refresh();
}
@ -194,4 +224,5 @@ frappe.ui.GroupBy = class {
get_group_by_fields() {
return this.report_view.meta.fields.filter((f)=> ["Select", "Link"].includes(f.fieldtype));
}
};

View file

@ -20,8 +20,6 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
this.report_name = route[3];
}
this.add_totals_row = this.view_user_settings.add_totals_row || 0;
if (this.report_name) {
return this.get_report_doc()
.then(doc => {
@ -35,6 +33,8 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
this.page_length = this.report_doc.json.page_length || 20;
this.order_by = this.report_doc.json.order_by || 'modified desc';
});
} else {
this.add_totals_row = this.view_user_settings.add_totals_row || 0;
}
}
@ -70,6 +70,13 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
//Setup groupby for reports
this.group_by_control = new frappe.ui.GroupBy(this);
if (this.report_doc && this.report_doc.json.group_by) {
this.group_by_control.apply_settings(this.report_doc.json.group_by);
}
if (this.view_user_settings && this.view_user_settings.group_by) {
this.group_by_control.apply_settings(this.view_user_settings.group_by);
}
}
get_args() {
@ -102,7 +109,8 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
order_by: this.sort_selector.get_sql_string(),
add_totals_row: this.add_totals_row,
page_length: this.page_length,
column_widths: this.get_column_widths()
column_widths: this.get_column_widths(),
group_by: this.group_by_control.get_settings()
}
let report_settings = {
@ -111,7 +119,8 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
order_by: this.report_doc.json.order_by,
add_totals_row: this.report_doc.json.add_totals_row,
page_length: this.report_doc.json.page_length,
column_widths: this.report_doc.json.column_widths
column_widths: this.report_doc.json.column_widths,
group_by: this.report_doc.json.group_by
}
if (!frappe.utils.deep_equal(current_settings, report_settings)) {
@ -129,7 +138,7 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
fields: this.fields,
filters: this.filter_area.get(),
order_by: this.sort_selector.get_sql_string(),
group_by: this.group_by,
group_by: this.group_by_control.get_settings(),
add_totals_row: this.add_totals_row
});
}
@ -997,7 +1006,8 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
order_by: this.sort_selector.get_sql_string(),
add_totals_row: this.add_totals_row,
page_length: this.page_length,
column_widths: this.get_column_widths()
column_widths: this.get_column_widths(),
group_by: this.group_by_control.get_settings()
}
return frappe.call({