csv-creator.ts 3.38 KB
Newer Older
1
import { Input } from "@angular/core";
2 3 4
import { LocalDataSource } from "ng2-smart-table";

import { ReportsSchema, TableColumn } from "./reports-schema";
5

6
export class CsvCreator {
7

8 9 10 11
    public source: LocalDataSource;
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
    @Input() settings: any;
    private reportSchema = new ReportsSchema();
12

13
    onSearch(query: string = "") {
14

15 16 17 18 19 20 21
        this.source.setFilter([
            // fields we want to include in the search
            {
                field: "name",
                search: query
            }
        ], false);
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
    export2Csv(): void {

        const columns: TableColumn[] = Array.from(this.columnMap.values());

        let encodedStr = columns.reduce((acct, current: TableColumn) => {

            if (current.isExport !== false) {
                return acct += "'" + current.title + "',";
            }
            else {
                return acct;
            }
        }, "");
        encodedStr = encodedStr.slice(0, -1);
        encodedStr += "\r\n";

        let fields: string[] = columns.reduce((acct, column: TableColumn) => {

            if (column.isExport !== false) {
                acct.push(column.field);
            }
            return acct;
        }, []);

        this.source.getAll().then((rows) => {

            rows.forEach((row) => {
                fields.forEach((field) => {
                    if (row.hasOwnProperty(field)) {
                        let value = row[field];

                        if (!value) {
                            value = "";
                        }
                        let valuePrepare = this.columnMap.get(field).valuePrepareFunction;
                        if (valuePrepare) {
                            value = valuePrepare.call(null, value, row);
                        }
                        encodedStr += "'" + value + "',";
                    }
                });
                encodedStr = encodedStr.slice(0, -1);
                encodedStr += "\r\n";
            });

            let a = document.createElement("a");
            a.setAttribute("style", "display:none;");
            document.body.appendChild(a);

            // Set utf-8 header to let excel recognize its encoding
            let blob = new Blob(["\ufeff", encodedStr], { type: "text/csv" });
            a.href = window.URL.createObjectURL(blob);
77
            a.download = (this.settings.fileName || "epalSystemReport") + "_all_stat" + ".csv";
78 79
            a.click();
        });
80 81 82
    }


83
    prepareColumnMap(): void {
84

85
        for (const key in this.settings.columns) {
86

87 88 89
            if (!this.settings.columns.hasOwnProperty(key)) {
                continue;
            }
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104
            const title: string = this.settings.columns[key]["title"];
            let column: TableColumn = new TableColumn();
            column.type = this.settings.columns[key]["type"];
            column.title = this.settings.columns[key]["title"];
            column.field = key;
            column.isDisplay = this.settings.columns[key]["isDisplay"];
            column.isExport = this.settings.columns[key]["isExport"];
            column.valuePrepareFunction = this.settings.columns[key]["valuePrepareFunction"];
            this.columnMap.set(column.field, column);

            if (this.settings.columns[key].isDisplay === false) {
                delete this.settings.columns[key];
            }
        }
105 106 107
    }

}