-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
grid-grouping.component.ts
358 lines (337 loc) · 11.9 KB
/
grid-grouping.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { Component, OnInit, } from '@angular/core';
import { ExcelExportService } from '@slickgrid-universal/excel-export';
import { TextExportService } from '@slickgrid-universal/text-export';
import {
AngularGridInstance,
Aggregators,
Column,
DelimiterType,
FieldType,
FileType,
Filters,
Formatters,
GridOption,
Grouping,
GroupTotalFormatters,
SortDirectionNumber,
SortComparers,
} from './../modules/angular-slickgrid';
@Component({
templateUrl: './grid-grouping.component.html'
})
export class GridGroupingComponent implements OnInit {
title = 'Example 14: Grouping & Aggregators';
subTitle = `
(<a href="https://ghiscoding.gitbook.io/angular-slickgrid/grid-functionalities/grouping-and-aggregators" target="_blank">Wiki docs</a>)
<ul>
<li>
Fully dynamic and interactive multi-level grouping with filtering and aggregates over 50'000 items
</li>
<li>Each grouping level can have its own aggregates (over child rows, child groups, or all descendant rows)..</li>
</ul>
`;
angularGrid!: AngularGridInstance;
columnDefinitions!: Column[];
gridOptions!: GridOption;
dataset!: any[];
gridObj: any;
dataviewObj: any;
processing = false;
excelExportService = new ExcelExportService();
textExportService = new TextExportService();
ngOnInit(): void {
this.initializeGrid();
}
initializeGrid() {
// add a simple button with event listener on 1st column for testing purposes
// a simple button with click event
const nameElementColumn1 = document.createElement('div');
const btn = document.createElement('button');
const btnLabel = document.createElement('span');
btnLabel.className = 'mdi mdi-help-circle no-padding';
btn.dataset.test = 'col1-hello-btn';
btn.className = 'btn btn-outline-secondary btn-xs btn-icon ms-1';
btn.textContent = 'Click me';
btn.title = 'simple column header test with a button click listener';
btn.addEventListener('click', () => alert('Hello World'));
btn.appendChild(btnLabel);
nameElementColumn1.appendChild(document.createTextNode('Id '));
nameElementColumn1.appendChild(btn);
this.columnDefinitions = [
{
id: 'sel', name: nameElementColumn1, field: 'num', type: FieldType.number,
columnPickerLabel: 'Custom Label', // add a custom label for the ColumnPicker/GridMenu when default header value extractor doesn't work for you ()
width: 140, maxWidth: 150,
excludeFromExport: true,
resizable: true,
filterable: true,
selectable: false,
focusable: false
},
{
id: 'title', name: 'Title', field: 'title',
width: 50,
minWidth: 50,
cssClass: 'cell-title',
filterable: true,
sortable: true
},
{
id: 'duration', name: 'Duration', field: 'duration',
minWidth: 50, width: 60,
filterable: true,
filter: { model: Filters.slider, operator: '>=' },
sortable: true,
type: FieldType.number,
groupTotalsFormatter: GroupTotalFormatters.sumTotals,
params: { groupFormatterPrefix: 'Total: ' }
},
{
id: '%', name: '% Complete', field: 'percentComplete',
minWidth: 70, width: 90,
formatter: Formatters.percentCompleteBar,
filterable: true,
filter: { model: Filters.compoundSlider },
sortable: true,
type: FieldType.number,
groupTotalsFormatter: GroupTotalFormatters.avgTotalsPercentage,
params: { groupFormatterPrefix: '<i>Avg</i>: ' }
},
{
id: 'start', name: 'Start', field: 'start',
minWidth: 60,
filterable: true,
filter: { model: Filters.compoundDate },
sortable: true,
type: FieldType.dateIso,
formatter: Formatters.dateIso,
exportWithFormatter: true
},
{
id: 'finish', name: 'Finish', field: 'finish',
minWidth: 60,
filterable: true,
filter: { model: Filters.compoundDate },
sortable: true,
type: FieldType.dateIso,
formatter: Formatters.dateIso,
exportWithFormatter: true
},
{
id: 'cost', name: 'Cost', field: 'cost',
minWidth: 70, width: 80,
sortable: true, filterable: true,
filter: { model: Filters.compoundInputNumber },
type: FieldType.number,
formatter: Formatters.currency,
groupTotalsFormatter: GroupTotalFormatters.sumTotalsCurrency,
params: { displayNegativeNumberWithParentheses: true, currencyPrefix: '€', groupFormatterCurrencyPrefix: '€', minDecimal: 2, maxDecimal: 4, groupFormatterPrefix: '<b>Total</b>: ' },
excelExportOptions: {
style: {
font: { outline: true, italic: true },
format: '€0.00##;[Red](€0.00##)',
},
width: 18
},
groupTotalsExcelExportOptions: {
style: {
alignment: { horizontal: 'center' },
font: { bold: true, color: 'FF005289', underline: 'single', fontName: 'Consolas', size: 10 },
fill: { type: 'pattern', patternType: 'solid', fgColor: 'FFE6F2F6' },
border: {
top: { color: 'FFa500ff', style: 'thick', },
left: { color: 'FFa500ff', style: 'medium', },
right: { color: 'FFa500ff', style: 'dotted', },
bottom: { color: 'FFa500ff', style: 'double', },
},
format: '"Total: "€0.00##;[Red]"Total: "(€0.00##)'
},
},
},
{
id: 'effort-driven', name: 'Effort Driven',
minWidth: 20, width: 80, maxWidth: 80,
cssClass: 'cell-effort-driven',
field: 'effortDriven',
formatter: Formatters.checkmarkMaterial,
sortable: true,
filterable: true,
filter: {
collection: [{ value: '', label: '' }, { value: true, label: 'True' }, { value: false, label: 'False' }],
model: Filters.singleSelect,
}
}
];
this.gridOptions = {
autoResize: {
container: '#demo-container',
rightPadding: 10
},
enableExcelExport: true,
enableFiltering: true,
// you could debounce/throttle the input text filter if you have lots of data
// filterTypingDebounce: 250,
enableGrouping: true,
enableTextExport: true,
gridMenu: {
hideExportTextDelimitedCommand: false
},
excelExportOptions: { sanitizeDataExport: true },
textExportOptions: { sanitizeDataExport: true },
externalResources: [this.excelExportService, this.textExportService],
showCustomFooter: true, // display some metrics in the bottom custom footer
customFooterOptions: {
// optionally display some text on the left footer container
hideMetrics: false,
hideTotalItemCount: false,
hideLastUpdateTimestamp: false
},
};
this.loadData(500);
}
angularGridReady(angularGrid: AngularGridInstance) {
this.angularGrid = angularGrid;
this.gridObj = angularGrid.slickGrid;
this.dataviewObj = angularGrid.dataView;
}
loadData(rowCount: number) {
// mock a dataset
const tmpData = [];
for (let i = 0; i < rowCount; i++) {
const randomYear = 2000 + Math.floor(Math.random() * 10);
const randomMonth = Math.floor(Math.random() * 11);
const randomDay = Math.floor((Math.random() * 29));
const randomPercent = Math.round(Math.random() * 100);
const randomCost = (i % 33 === 0) ? null : Math.round(Math.random() * 10000) / 100;
tmpData[i] = {
id: 'id_' + i,
num: i,
title: 'Task ' + i,
duration: Math.round(Math.random() * 100) + '',
percentComplete: randomPercent,
percentCompleteNumber: randomPercent,
start: new Date(randomYear, randomMonth, randomDay),
finish: new Date(randomYear, (randomMonth + 1), randomDay),
cost: i % 3 ? randomCost : randomCost !== null ? -randomCost : null,
effortDriven: (i % 5 === 0)
};
}
this.dataset = tmpData;
}
clearGrouping() {
this.dataviewObj.setGrouping([]);
}
collapseAllGroups() {
this.dataviewObj.collapseAllGroups();
}
expandAllGroups() {
this.dataviewObj.expandAllGroups();
}
exportToExcel() {
this.excelExportService.exportToExcel({
filename: 'Export',
format: FileType.xlsx
});
}
exportToFile(type = 'csv') {
this.textExportService.exportToFile({
delimiter: (type === 'csv') ? DelimiterType.comma : DelimiterType.tab,
filename: 'myExport',
format: (type === 'csv') ? FileType.csv : FileType.txt
});
}
groupByDuration() {
this.dataviewObj.setGrouping({
getter: 'duration',
formatter: (g) => `Duration: ${g.value} <span style="color:green">(${g.count} items)</span>`,
aggregators: [
new Aggregators.Avg('percentComplete'),
new Aggregators.Sum('cost')
],
comparer: (a, b) => SortComparers.numeric(a.value, b.value, SortDirectionNumber.asc),
aggregateCollapsed: false,
lazyTotalsCalculation: true
} as Grouping);
// you need to manually add the sort icon(s) in UI
this.angularGrid.filterService.setSortColumnIcons([{ columnId: 'duration', sortAsc: true }]);
this.gridObj.invalidate(); // invalidate all rows and re-render
}
groupByDurationOrderByCount(aggregateCollapsed: boolean) {
this.angularGrid.filterService.setSortColumnIcons([]);
this.dataviewObj.setGrouping({
getter: 'duration',
formatter: (g) => `Duration: ${g.value} <span style="color:green">(${g.count} items)</span>`,
comparer: (a, b) => {
return a.count - b.count;
},
aggregators: [
new Aggregators.Avg('percentComplete'),
new Aggregators.Sum('cost')
],
aggregateCollapsed,
lazyTotalsCalculation: true
} as Grouping);
this.gridObj.invalidate(); // invalidate all rows and re-render
}
groupByDurationEffortDriven() {
this.angularGrid.filterService.setSortColumnIcons([]);
this.dataviewObj.setGrouping([
{
getter: 'duration',
formatter: (g) => `Duration: ${g.value} <span style="color:green">(${g.count} items)</span>`,
aggregators: [
new Aggregators.Sum('duration'),
new Aggregators.Sum('cost')
],
aggregateCollapsed: true,
lazyTotalsCalculation: true
},
{
getter: 'effortDriven',
formatter: (g) => `Effort-Driven: ${(g.value ? 'True' : 'False')} <span style="color:green">(${g.count} items)</span>`,
aggregators: [
new Aggregators.Avg('percentComplete'),
new Aggregators.Sum('cost')
],
collapsed: true,
lazyTotalsCalculation: true
}
] as Grouping[]);
this.gridObj.invalidate(); // invalidate all rows and re-render
}
groupByDurationEffortDrivenPercent() {
this.angularGrid.filterService.setSortColumnIcons([]);
this.dataviewObj.setGrouping([
{
getter: 'duration',
formatter: (g) => `Duration: ${g.value} <span style="color:green">(${g.count} items)</span>`,
aggregators: [
new Aggregators.Sum('duration'),
new Aggregators.Sum('cost')
],
aggregateCollapsed: true,
lazyTotalsCalculation: true
},
{
getter: 'effortDriven',
formatter: (g) => `Effort-Driven: ${(g.value ? 'True' : 'False')} <span style="color:green">(${g.count} items)</span>`,
aggregators: [
new Aggregators.Sum('duration'),
new Aggregators.Sum('cost')
],
lazyTotalsCalculation: true
},
{
getter: 'percentComplete',
formatter: (g) => `% Complete: ${g.value} <span style="color:green">(${g.count} items)</span>`,
aggregators: [
new Aggregators.Avg('percentComplete')
],
aggregateCollapsed: true,
collapsed: true,
lazyTotalsCalculation: true
}
] as Grouping[]);
this.gridObj.invalidate(); // invalidate all rows and re-render
}
}