Skip to content

Commit

Permalink
Merge pull request #108 from LironT/master
Browse files Browse the repository at this point in the history
added support in column order
  • Loading branch information
asafdav committed Aug 12, 2015
2 parents e84432f + 39252f0 commit afb17b9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ngCsv attributes
<button type="button" ng-csv="getArray()" csv-header="['Field A', 'Field B', 'Field C']" filename="test.csv">Export</button>
```
* 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
Expand Down
3 changes: 2 additions & 1 deletion src/ng-csv/directives/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ angular.module('ngCsv.directives').
data: '&ngCsv',
filename: '@filename',
header: '&csvHeader',
columnOrder: '&csvColumnOrder',
txtDelim: '@textDelimiter',
decimalSep: '@decimalSeparator',
quoteStrings: '@quoteStrings',
Expand Down Expand Up @@ -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 : ",";

Expand Down
6 changes: 4 additions & 2 deletions src/ng-csv/services/csv-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : ",");
Expand Down
21 changes: 21 additions & 0 deletions test/unit/ngCsv/directives/ngCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<div ng-csv="testDelim" csv-column-order="order" filename="custom.csv"></div>')($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(
Expand Down

0 comments on commit afb17b9

Please sign in to comment.