Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit edea2ef

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(export): add onGrid Before/After ExportToFile Observable
- this could be useful if user want to display a spinner while downloading
1 parent faff439 commit edea2ef

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/app/examples/grid-grouping.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ <h2>{{title}}</h2>
3838
<button class="btn btn-default btn-xs" (click)="groupByDurationEffortDrivenPercent()">
3939
Group by duration then effort-driven then percent.
4040
</button>
41+
<span [hidden]="!processing">
42+
<i class="fa fa-refresh fa-spin fa-lg fa-fw"></i>
43+
</span>
4144
</div>
45+
4246
<angular-slickgrid gridId="grid2" (onDataviewCreated)="dataviewReady($event)" (onGridCreated)="gridReady($event)" [columnDefinitions]="columnDefinitions"
4347
[gridOptions]="gridOptions" [dataset]="dataset">
4448
</angular-slickgrid>

src/app/examples/grid-grouping.component.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { Aggregators, Column, FieldType, Formatter, Formatters, GridOption, Sorters } from './../modules/angular-slickgrid';
1+
import { Component, Injectable, OnInit, OnDestroy } from '@angular/core';
2+
import { Aggregators, Column, ExportService, FieldType, Formatter, Formatters, GridOption, Sorters } from './../modules/angular-slickgrid';
3+
import { Subscription } from 'rxjs/Subscription';
34

5+
@Injectable()
46
@Component({
57
templateUrl: './grid-grouping.component.html'
68
})
7-
export class GridGroupingComponent implements OnInit {
9+
export class GridGroupingComponent implements OnInit, OnDestroy {
810
title = 'Example 14: Grouping';
911
subTitle = `
1012
<ul>
@@ -18,6 +20,20 @@ export class GridGroupingComponent implements OnInit {
1820
dataset: any[];
1921
gridObj: any;
2022
dataviewObj: any;
23+
processing = false;
24+
exportBeforeSub: Subscription;
25+
exportAfterSub: Subscription;
26+
27+
constructor(private exportService: ExportService) {
28+
// display a spinner while downloading
29+
this.exportBeforeSub = this.exportService.onGridBeforeExportToFile.subscribe(() => this.processing = true);
30+
this.exportAfterSub = this.exportService.onGridAfterExportToFile.subscribe(() => this.processing = false);
31+
}
32+
33+
ngOnDestroy() {
34+
this.exportBeforeSub.unsubscribe();
35+
this.exportAfterSub.unsubscribe();
36+
}
2137

2238
ngOnInit(): void {
2339
this.columnDefinitions = [

src/app/modules/angular-slickgrid/services/export.service.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable } from '@angular/core';
2+
import { TranslateService } from '@ngx-translate/core';
23
import {
34
CellArgs,
45
CustomGridMenu,
@@ -14,8 +15,8 @@ import {
1415
HeaderMenuOnCommandArgs,
1516
HeaderMenuOnBeforeMenuShowArgs
1617
} from './../models/index';
17-
import { TranslateService } from '@ngx-translate/core';
1818
import { addWhiteSpaces, htmlEntityDecode } from './../services/utilities';
19+
import { Subject } from 'rxjs/Subject';
1920
import { TextEncoder } from 'text-encoding-utf-8';
2021

2122
// using external non-typed js libraries
@@ -38,6 +39,8 @@ export class ExportService {
3839
private _gridOptions: GridOption;
3940
private _hasGroupedItems = false;
4041
private _exportOptions: ExportOption;
42+
onGridBeforeExportToFile = new Subject<boolean>();
43+
onGridAfterExportToFile = new Subject<{ options: any }>();
4144

4245
constructor(private translate: TranslateService) { }
4346

@@ -63,18 +66,24 @@ export class ExportService {
6366
* Example: exportToFile({ format: FileType.csv, delimiter: DelimiterType.comma })
6467
*/
6568
exportToFile(options: ExportOption) {
69+
this.onGridBeforeExportToFile.next(true);
6670
this._exportOptions = $.extend(true, {}, this._gridOptions.exportOptions, options);
6771

6872
// get the CSV output from the grid data
6973
const dataOutput = this.getDataOutput();
7074

7175
// trigger a download file
72-
this.startDownloadFile({
73-
filename: `${this._exportOptions.filename}.${this._exportOptions.format}`,
74-
csvContent: dataOutput,
75-
format: this._exportOptions.format,
76-
useUtf8WithBom: this._exportOptions.useUtf8WithBom
77-
});
76+
// wrap it into a setTimeout so that the EventAggregator has enough time to start a pre-process like showing a spinner
77+
setTimeout(() => {
78+
const downloadOptions = {
79+
filename: `${this._exportOptions.filename}.${this._exportOptions.format}`,
80+
csvContent: dataOutput,
81+
format: this._exportOptions.format,
82+
useUtf8WithBom: this._exportOptions.useUtf8WithBom
83+
};
84+
this.startDownloadFile(downloadOptions);
85+
this.onGridAfterExportToFile.next({ options: downloadOptions });
86+
}, 0);
7887
}
7988

8089
// -----------------------

0 commit comments

Comments
 (0)