From 479d10d79d870649519ab7cda889c57b7b6c1174 Mon Sep 17 00:00:00 2001 From: liront Date: Wed, 12 Aug 2015 11:35:11 +0300 Subject: [PATCH 1/2] added support in column order --- README.md | 1 + src/ng-csv/directives/ng-csv.js | 3 ++- src/ng-csv/services/csv-service.js | 6 ++++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 369fe99..1fe41d0 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ ngCsv attributes ``` +* csv-column-order: Defines the column order to be set when creating the body of the CSV (may be according to the csv-headers) - use it when you have an array of objects. * field-separator: Defines the field separator character (default is ,) * decimal-separator: Defines the decimal separator character (default is .). If set to "locale", it uses the [language sensitive representation of the number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). * text-delimiter: If provided, will use this characters to "escape" fields, otherwise will use double quotes as deafult diff --git a/src/ng-csv/directives/ng-csv.js b/src/ng-csv/directives/ng-csv.js index 603e193..405b084 100644 --- a/src/ng-csv/directives/ng-csv.js +++ b/src/ng-csv/directives/ng-csv.js @@ -12,6 +12,7 @@ angular.module('ngCsv.directives'). data: '&ngCsv', filename: '@filename', header: '&csvHeader', + columnOrder: '&csvColumnOrder', txtDelim: '@textDelimiter', decimalSep: '@decimalSeparator', quoteStrings: '@quoteStrings', @@ -49,7 +50,7 @@ angular.module('ngCsv.directives'). addByteOrderMarker: $scope.addByteOrderMarker }; if (angular.isDefined($attrs.csvHeader)) options.header = $scope.$eval($scope.header); - + if (angular.isDefined($attrs.csvColumnOrder)) options.columnOrder = $scope.$eval($scope.columnOrder); 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 ababe06..2ef0cb7 100644 --- a/src/ng-csv/services/csv-service.js +++ b/src/ng-csv/services/csv-service.js @@ -102,8 +102,10 @@ angular.module('ngCsv.services'). infoArray = []; - angular.forEach(row, function (field, key) { - this.push(that.stringifyField(field, options)); + var iterator = !!options.columnOrder ? options.columnOrder : row; + angular.forEach(iterator, function (field, key) { + var val = !!options.columnOrder ? row[field] : field; + this.push(that.stringifyField(val, options)); }, infoArray); dataString = infoArray.join(options.fieldSep ? options.fieldSep : ","); From 39252f0876cff542b5a6826e7bb7352f7fc2ecd2 Mon Sep 17 00:00:00 2001 From: liront Date: Wed, 12 Aug 2015 12:06:16 +0300 Subject: [PATCH 2/2] added a test for column order --- test/unit/ngCsv/directives/ngCsv.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/unit/ngCsv/directives/ngCsv.js b/test/unit/ngCsv/directives/ngCsv.js index 407f48a..75fc132 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('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' ]; + 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); + expect(scope.$eval(scope.columnOrder)).toEqual($rootScope.order); + + scope.buildCSV(scope.data).then(function() { + expect(scope.csv).toBe('2,1,3\r\n5,4,6\r\n'); + done(); + }); + scope.$apply(); + }); + it('Accepts optional text-delimiter attribute (input array)', function (done) { $rootScope.testDelim = [[1, 'a', 2], ['b', 'c', 3]]; var element = $compile(