Commit f2bc7621 authored by Alessandro Curci's avatar Alessandro Curci
Browse files

implemented CSV export function

parent d54e2b97
......@@ -3038,6 +3038,15 @@
"integrity": "sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==",
"dev": true
},
"@types/json2csv": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/@types/json2csv/-/json2csv-5.0.1.tgz",
"integrity": "sha512-1r5GCTyFtdQ53CRSIctzWZCmtDXvxtzM77SzOqPB4woMeGcc3rhUMzPqEQH3rokG1k/QLzlC5Qe5Ih8NuFN70Q==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/lodash": {
"version": "4.14.123",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.123.tgz",
......@@ -6842,6 +6851,11 @@
"schema-utils": "^2.6.5"
}
},
"file-saver": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.2.tgz",
"integrity": "sha512-Wz3c3XQ5xroCxd1G8b7yL0Ehkf0TC9oYC6buPFkNnU9EnaPlifeAFCyCh+iewXTyFRcg0a6j3J7FmJsIhlhBdw=="
},
"fileset": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/fileset/-/fileset-2.0.3.tgz",
......@@ -8787,6 +8801,23 @@
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=",
"dev": true
},
"json2csv": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/json2csv/-/json2csv-5.0.1.tgz",
"integrity": "sha512-QFMifUX1y8W2tKi2TwZpnzf2rHdZvzdmgZUMEMDF46F90f4a9mUeWfx/qg4kzXSZYJYc3cWA5O+eLXk5lj9g8g==",
"requires": {
"commander": "^5.0.0",
"jsonparse": "^1.3.1",
"lodash.get": "^4.4.2"
},
"dependencies": {
"commander": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz",
"integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg=="
}
}
},
"json3": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/json3/-/json3-3.3.3.tgz",
......@@ -8822,8 +8853,7 @@
"jsonparse": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz",
"integrity": "sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=",
"dev": true
"integrity": "sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA="
},
"jsprim": {
"version": "1.4.1",
......@@ -9276,6 +9306,11 @@
"integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=",
"dev": true
},
"lodash.get": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk="
},
"lodash.memoize": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz",
......
import { Injectable } from '@angular/core';
import * as json2csv from 'json2csv'; // convert json file to csv
import { saveAs } from 'file-saver'; // save the file
@Injectable()
export class DownloadCsvService {
Json2csvParser = json2csv.Parser;
constructor() {
}
public downloadFile(data: any, opts, filename = 'data.csv') {
const csvData = this.convertToCSV(data, opts);
const file = new Blob([csvData], { type: 'text/csv;charset=utf-8' });
saveAs(file, filename);
}
public convertToCSV(objArray: any, opts) {
const json2csvParser = new this.Json2csvParser(opts);
const csv = json2csvParser.parse(objArray);
console.log(csv);
return csv;
}
}
......@@ -13,6 +13,7 @@ export interface ListTPL {
export class ListTPLOptions {
supports_creation?: boolean;
creation_params?: any;
csv_export_opts?: any;
row_detail?: {
title: string,
type: string,
......
......@@ -964,6 +964,10 @@ export class AppConfig {
'organization': 'organization__id',
'person': 'person__id',
},
csv_export_opts: { // see https://github.com/zemirco/json2csv for available options
delimiter: '\t',
fields: ['id','organization.name','person.name','person.birth_date','person.birth_location','role','start_date','end_date']
},
row_detail: {
title: 'modifica incarico',
type: 'form',
......
......@@ -104,6 +104,7 @@ library.add(
dom.watch();
import {SnotifyModule, SnotifyService, ToastDefaults} from 'ng-snotify';
import {DownloadCsvService} from './_services/download-csv.service';
@NgModule({
imports: [
......@@ -125,6 +126,7 @@ import {SnotifyModule, SnotifyService, ToastDefaults} from 'ng-snotify';
AuthGuard,
AuthenticationService,
APICommonService,
DownloadCsvService,
DatePipe,
FormBuilder,
{
......
<div class="mb-4 clearfix" *ngIf="TPLoptions?.supports_creation">
<div class="clearfix">
<div class="mb-4 float-right" *ngIf="TPLoptions?.supports_creation">
<a [routerLink]="'/' + apipath + '/edit'" [queryParams]="creationQuery" class="btn btn-primary float-right">Crea</a>
</div>
<div class="mb-4 mr-3 float-right" *ngIf="TPLoptions?.csv_export_opts">
<button class="btn btn-primary" (click)="exportCSV()">Esporta CSV</button>
</div>
</div>
<app-filters-panel *ngIf="!selected.length" [fields]="filters" [model]="filterQuery" [form]="filterForm" (filterchange)="setFilters($event)"></app-filters-panel>
......
......@@ -15,6 +15,7 @@ import intersectionBy from 'lodash-es/intersectionBy';
// APP SERVICES
import {APICommonService} from '../../../_services/api-common.service';
import {DownloadCsvService} from '../../../_services/download-csv.service';
import {SnotifyService} from 'ng-snotify';
// APP CONFIG
......@@ -88,6 +89,7 @@ export class AbstractListComponent implements OnDestroy, OnInit {
private route: ActivatedRoute,
private titleService: Title,
private snotifyService: SnotifyService,
public downloadService: DownloadCsvService,
public config: AppConfig
) {
// router events subscription
......@@ -352,6 +354,10 @@ export class AbstractListComponent implements OnDestroy, OnInit {
page: 1
};
}
public exportCSV() {
this.downloadService.downloadFile(this.rows, this.TPLoptions.csv_export_opts);
}
}
/*updateValue(event, cell, rowIndex) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment