From 67a88a4bb882871ab014f7754c634e5c35fad452 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 2 Oct 2015 16:35:34 -0400 Subject: [PATCH 1/2] added label attribute to define whether or not using keys as column value in the CSV file --- README.md | 2 +- src/ng-csv/directives/ng-csv.js | 4 +++- src/ng-csv/services/csv-service.js | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1fe41d0..f7ada4c 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ ngCsv attributes * lazy-load: If defined and set to true, ngCsv will generate the data string only on demand. See the lazy_load example for more details. * add-bom: Add the Byte Order Mark, use this option if you are getting an unexpected char when opening the file on any windows App. * charset: Defines the charset of the downloadable Csv file. Default is "utf-8". - +* csv-label: Defines whether or not using keys as csv column value (default is false). ## Examples You can check out this live example here: https://asafdav.github.io/ng-csv/example/ diff --git a/src/ng-csv/directives/ng-csv.js b/src/ng-csv/directives/ng-csv.js index 22f734b..bfb80f9 100644 --- a/src/ng-csv/directives/ng-csv.js +++ b/src/ng-csv/directives/ng-csv.js @@ -20,7 +20,8 @@ angular.module('ngCsv.directives'). lazyLoad: '@lazyLoad', addByteOrderMarker: "@addBom", ngClick: '&', - charset: '@charset' + charset: '@charset', + label: '&csvLabel' }, controller: [ '$scope', @@ -51,6 +52,7 @@ angular.module('ngCsv.directives'). }; if (angular.isDefined($attrs.csvHeader)) options.header = $scope.$eval($scope.header); if (angular.isDefined($attrs.csvColumnOrder)) options.columnOrder = $scope.$eval($scope.columnOrder); + if (angular.isDefined($attrs.csvLabel)) options.label = $scope.$eval($scope.label); options.fieldSep = $scope.fieldSep ? $scope.fieldSep : ","; diff --git a/src/ng-csv/services/csv-service.js b/src/ng-csv/services/csv-service.js index 2ef0cb7..d8315de 100644 --- a/src/ng-csv/services/csv-service.js +++ b/src/ng-csv/services/csv-service.js @@ -87,6 +87,18 @@ angular.module('ngCsv.services'). csvContent += headerString + EOL; } + // Check if using keys as labels + if (angular.isDefined(options.label) && options.label && typeof options.label === 'boolean') { + var encodingArray, labelString; + + encodingArray = []; + angular.forEach(arrData[0], function(value, label) { + this.push(that.stringifyField(label, options)); + }, encodingArray); + labelString = encodingArray.join(options.fieldSep ? options.fieldSep : ","); + csvContent += labelString + EOL; + } + var arrData = []; if (angular.isArray(responseData)) { From 4ed3e827c079c76ff8ce1182e970afc87ee8ded7 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Oct 2015 14:05:32 -0400 Subject: [PATCH 2/2] added unit test for csv-label case' --- src/ng-csv/services/csv-service.js | 18 +++++++++--------- test/unit/ngCsv/directives/ngCsv.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/ng-csv/services/csv-service.js b/src/ng-csv/services/csv-service.js index d8315de..13a1a2b 100644 --- a/src/ng-csv/services/csv-service.js +++ b/src/ng-csv/services/csv-service.js @@ -87,6 +87,15 @@ angular.module('ngCsv.services'). csvContent += headerString + EOL; } + var arrData = []; + + if (angular.isArray(responseData)) { + arrData = responseData; + } + else if (angular.isFunction(responseData)) { + arrData = responseData(); + } + // Check if using keys as labels if (angular.isDefined(options.label) && options.label && typeof options.label === 'boolean') { var encodingArray, labelString; @@ -99,15 +108,6 @@ angular.module('ngCsv.services'). csvContent += labelString + EOL; } - var arrData = []; - - if (angular.isArray(responseData)) { - arrData = responseData; - } - else if (angular.isFunction(responseData)) { - arrData = responseData(); - } - angular.forEach(arrData, function (oldRow, index) { var row = angular.copy(arrData[index]); var dataString, infoArray; diff --git a/test/unit/ngCsv/directives/ngCsv.js b/test/unit/ngCsv/directives/ngCsv.js index 75fc132..5862708 100644 --- a/test/unit/ngCsv/directives/ngCsv.js +++ b/test/unit/ngCsv/directives/ngCsv.js @@ -129,6 +129,27 @@ describe('ngCsv directive', function () { scope.$apply(); }); + it('Creates a header row using keys if csv-label sets to true', function (done) { + // Compile a piece of HTML containing the directive + $rootScope.testDelim = [ {a:1, b:2, c:3}, {a:4, b:5, c:6} ]; + var element = $compile( + '
')($rootScope); + + $rootScope.$digest(); + + var scope = element.isolateScope(); + + // Check that the compiled element contains the templated content + expect(scope.$eval(scope.data)).toEqual($rootScope.testDelim); + + + scope.buildCSV(scope.data).then(function() { + expect(scope.csv).toBe('a,b,c\r\n1,2,3\r\n4,5,6\r\n'); + done(); + }); + scope.$apply(); + }); + it('Accepts optional csv-column-order attribute (input array)', function (done) { $rootScope.testDelim = [ {a:1, b:2, c:3}, {a:4, b:5, c:6} ]; $rootScope.order = [ 'b', 'a', 'c' ];