Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ngCsv attributes
* 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).
* sort-column: Defines the column name that the data needs to be sorted by. If you pass the column name with '-' in front of it, it will reverse the sort.

## Examples
You can check out this live example here: https://asafdav.github.io/ng-csv/example/
Expand All @@ -78,4 +79,3 @@ Supported Browsers
| IE 10+ | Yes |

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/asafdav/ng-csv/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

18 changes: 14 additions & 4 deletions build/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.ex
* Created by asafdav on 15/05/14.
*/
angular.module('ngCsv.services').
service('CSV', ['$q', function ($q) {
service('CSV', ['$q', '$filter', function ($q, $filter) {

var EOL = '\r\n';
var BOM = "\ufeff";
Expand Down Expand Up @@ -130,13 +130,21 @@ angular.module('ngCsv.services').
arrData = responseData();
}

// Sorts the data
if(angular.isDefined(options.sortColumn)) {
arrData = $filter('orderBy')(arrData, options.sortColumn);
}

// Check if using keys as labels
if (angular.isDefined(options.label) && options.label && typeof options.label === 'boolean') {
var labelArray, labelString;

labelArray = [];
angular.forEach(arrData[0], function(value, label) {
this.push(that.stringifyField(label, options));

var iterator = !!options.columnOrder ? options.columnOrder : arrData[0];
angular.forEach(iterator, function(value, label) {
var val = !!options.columnOrder ? value : label;
this.push(that.stringifyField(val, options));
}, labelArray);
labelString = labelArray.join(options.fieldSep ? options.fieldSep : ",");
csvContent += labelString + EOL;
Expand Down Expand Up @@ -221,7 +229,8 @@ angular.module('ngCsv.directives').
addByteOrderMarker: "@addBom",
ngClick: '&',
charset: '@charset',
label: '&csvLabel'
label: '&csvLabel',
sortColumn: '@sortColumn'
},
controller: [
'$scope',
Expand Down Expand Up @@ -253,6 +262,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);
if (angular.isDefined($attrs.sortColumn)) options.sortColumn = $scope.sortColumn;

options.fieldSep = $scope.fieldSep ? $scope.fieldSep : ",";

Expand Down
4 changes: 2 additions & 2 deletions build/ng-csv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading