src/app/core/export/export-data-directive/export-data.directive.ts
A directive that can be attached to a html element, commonly a button. Usage:
Example : <button
mat-stroked-button
[appExportData]="data"
format="csv"
>
Export CSV
</button
Selector | [appExportData] |
Standalone | true |
Methods |
Inputs |
HostListeners |
constructor(downloadService: DownloadService)
|
||||||
Parameters :
|
appExportData | |
Type : any
|
|
Default value : []
|
|
data to be exported |
exportConfig | |
Type : ExportColumnConfig[]
|
|
(Optional) definition of fields to be exported. If not provided, all properties will be included in the export. |
filename | |
Type : string
|
|
Default value : "exportedData"
|
|
filename for the download of the exported data |
format | |
Type : FileDownloadFormat
|
|
Default value : "csv"
|
|
What kind of data should be export? Currently implemented are 'json', 'csv' |
click |
click |
click()
|
Decorators :
@HostListener('click')
|
Returns :
any
|
import { Directive, HostListener, Input } from "@angular/core";
import { ExportColumnConfig } from "../data-transformation-service/export-column-config";
import {
DownloadService,
FileDownloadFormat,
} from "../download-service/download.service";
/**
* A directive that can be attached to a html element, commonly a button.
* Usage:
* ```html
* <button
* mat-stroked-button
* [appExportData]="data"
* format="csv"
* >
* Export CSV
* </button
*
* ```
*/
@Directive({
selector: "[appExportData]",
standalone: true,
})
export class ExportDataDirective {
/** data to be exported */
@Input("appExportData") data: any = [];
/** What kind of data should be export? Currently implemented are 'json', 'csv' */
@Input() format: FileDownloadFormat = "csv";
/** filename for the download of the exported data */
@Input() filename: string = "exportedData";
/**
* (Optional) definition of fields to be exported.
*
* If not provided, all properties will be included in the export.
*/
@Input() exportConfig: ExportColumnConfig[];
constructor(private downloadService: DownloadService) {}
@HostListener("click")
click() {
return this.downloadService.triggerDownload(
this.data,
this.format,
this.filename,
this.exportConfig,
);
}
}