+
+
+
+
`);
+ this.page.wrapper.find(".sort-selector").before(group_by_button);
+ group_by_button.click(() => this.groupby_edit_area.show());
+ }
+
+ 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.aggregate_on = this.page.wrapper.find('.aggregate-on option:selected').val();
+
+ //All necessary fields must be set before applying group by
+ if(!this.group_by) {
+ this.page.wrapper.find('.groupby').focus();
+ return;
+ } else if(!this.aggregate_function) {
+ this.page.wrapper.find('.aggregate-function').focus();
+ return;
+ } else if(!this.aggregate_on && this.aggregate_function!=='count') {
+ this.page.wrapper.find('.aggregate-on').focus();
+ return;
+ }
+
+ //If chosen group by field is not in fields, push it to fields
+ if(!this.report_view.columns.includes(this.group_by)) {
+ this.report_view.fields.push(this.group_by);
+ }
+
+ //If function is count add a new field for count
+ if(this.aggregate_function === 'count') {
+ let group_by_query = 'count(' + this.report_view.field_type + '.'+ this.group_by+') as ' + this.group_by + '_Count';
+ this.report_view.fields.push([group_by_query, this.doctype]);
+ this.order_by = this.group_by + '_Count desc';
+ } else {
+ //If chosen 'aggregate on' field is not in fields, push it to fields
+ if(!this.report_view.columns.includes(this.aggregate_on)) {
+ this.report_view.fields.push(this.aggregate_on);
+ }
+ this.order_by = this.aggregate_on + ' desc';
+ }
+
+ $('.set-groupby-and-run').hide();
+
+ //Get current columns so that they can be restored when group by is removed
+ this.current_cols = this.report_view.columns;
+
+ let remove_columns = this.report_view.columns.filter(column => ![this.aggregate_on, this.group_by].includes(column.field));
+ remove_columns.forEach((col) => {
+ this.report_view.remove_column_from_datatable(col);
+ });
+
+ if(this.report_view.columns[0]) {
+ this.removed_previous_width = this.report_view.columns[0].docfield.width;
+ this.report_view.columns[0].docfield.width = 400;
+ }
+ }
+
+ remove_group_by() {
+ this.report_view.columns[0].docfield.width = this.removed_previous_width;
+ this.order_by = '';
+ this.groupby_edit_area.hide();
+ $('.set-groupby-and-run').show();
+ this.group_by = null;
+ this.aggregate_function = null;
+ this.aggregate_on = null;
+ $(".groupby").val("");
+ $(".aggregate-function").val("count");
+ $(".aggregate-on").val("").hide();
+ //Add the removed columns
+ this.current_cols.forEach((col, i) => {
+ this.report_view.add_column_to_datatable(col.field, this.doctype, i);
+ });
+ this.report_view.fields.pop();
+ this.report_view.setup_columns();
+ }
+
+ get_group_by_fields() {
+ return this.report_view.meta.fields.filter((f)=> ["Select", "Link"].includes(f.fieldtype));
+ }
+};
diff --git a/frappe/public/js/frappe/views/reports/report_view.js b/frappe/public/js/frappe/views/reports/report_view.js
index b60e4e6e15..59bd32c61c 100644
--- a/frappe/public/js/frappe/views/reports/report_view.js
+++ b/frappe/public/js/frappe/views/reports/report_view.js
@@ -68,116 +68,15 @@ frappe.views.ReportView = class ReportView extends frappe.views.ListView {
});
//Setup groupby for reports
- this.setup_groupby_area();
+ this.group_by_control = new frappe.ui.GroupBy(this);
}
- make_groupby_button() {
- let group_by_button = $(`