diff options
Diffstat (limited to 'catalog-ui/src/app/directives/export-json-to-excel')
-rw-r--r-- | catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html | 7 | ||||
-rw-r--r-- | catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts | 61 |
2 files changed, 68 insertions, 0 deletions
diff --git a/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html new file mode 100644 index 0000000000..55696b33fa --- /dev/null +++ b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html @@ -0,0 +1,7 @@ +<span data-ng-click="export()" + class="sprite-new download-btn hand" + uib-tooltip="Download" + tooltip-class="uib-custom-tooltip" + tooltip-placement="bottom" + data-tests-id="download-excel"></span> + diff --git a/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts new file mode 100644 index 0000000000..deb1c94936 --- /dev/null +++ b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts @@ -0,0 +1,61 @@ +'use strict'; +import {FileUtils} from "app/utils"; + +export interface IJsonExportExcelScope extends ng.IScope { + initExportExcelData:Function; + export:()=>void; +} + +export class JsonExportExcelDirective implements ng.IDirective { + + constructor(private fileUtils:FileUtils) { + } + + scope = { + initExportExcelData: '&'//get function that init and returns Models.ExportExcel + }; + + public restrict = 'E'; + replace = true; + template = ():string => { + return require('./export-json-to-excel.html'); + }; + + private jsonToTableText = (headers:string, dataObj:Array<Object>):string=> { + let tableText = headers + "\n"; + _.each(dataObj, (rowData:any) => { + tableText += _.values(rowData).join() + "\n"; + }); + return tableText; + }; + + link = (scope:IJsonExportExcelScope, $elem:any) => { + scope.export = ():void => { + let exportExcelData = scope.initExportExcelData(); + exportExcelData.fileName = !!exportExcelData.fileName ? exportExcelData.fileName : 'export-excel'; + + let headers = exportExcelData.tableHeaders.join(","); + let tableData = ""; + if (exportExcelData.groupByField) { + _.each(_.groupBy(exportExcelData.dataObj, exportExcelData.groupByField), (groupData:Array<Object>) => { + tableData += this.jsonToTableText(headers, groupData); + tableData += '\n'; + }); + } else { + tableData = this.jsonToTableText(headers, exportExcelData.dataObj); + } + + let blob = new Blob([exportExcelData.metaData.join('\n\n') + '\n\n' + tableData], {type: "text/csv;charset=utf-8"}); + + return this.fileUtils.downloadFile(blob, exportExcelData.fileName + '.csv'); + }; + + }; + + public static factory = (fileUtils:FileUtils)=> { + return new JsonExportExcelDirective( fileUtils); + }; + +} + +JsonExportExcelDirective.factory.$inject = ['FileUtils']; |