Skip to content

Commit

Permalink
Merge pull request #122 from shaohaolin/master
Browse files Browse the repository at this point in the history
Added label attribute to define whether or not using keys as column value
  • Loading branch information
asafdav committed Oct 10, 2015
2 parents 0e333e5 + 4ed3e82 commit 0ef8feb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
4 changes: 3 additions & 1 deletion src/ng-csv/directives/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ angular.module('ngCsv.directives').
lazyLoad: '@lazyLoad',
addByteOrderMarker: "@addBom",
ngClick: '&',
charset: '@charset'
charset: '@charset',
label: '&csvLabel'
},
controller: [
'$scope',
Expand Down Expand Up @@ -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 : ",";

Expand Down
12 changes: 12 additions & 0 deletions src/ng-csv/services/csv-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ angular.module('ngCsv.services').
arrData = responseData();
}

// 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;
}

angular.forEach(arrData, function (oldRow, index) {
var row = angular.copy(arrData[index]);
var dataString, infoArray;
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('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(
'<div ng-csv="testDelim" csv-label="true" 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);


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' ];
Expand Down

0 comments on commit 0ef8feb

Please sign in to comment.