Initial commit for the Angular part. We are nowhere yet.
This commit is contained in:
18
bookie/src/app/shared/to-csv.service.ts
Normal file
18
bookie/src/app/shared/to-csv.service.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ToCsvService {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
toCsv(headers: any, data: any[]): string {
|
||||
const header = Object.keys(headers);
|
||||
const replacer = (key, value) => value === null ? '' : value;
|
||||
const csv = data.map(row => header.map(fieldName => JSON.stringify(row[headers[fieldName]], replacer)).join(','));
|
||||
csv.unshift(header.join(','));
|
||||
return csv.join('\r\n');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user