Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to sort the data by a particular column name. #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Added option to sort the data by a particular column name.
Murali Srinivasan committed Mar 18, 2016
commit f862d60f80c2ec8e8bc724ebe5621e045d060d1a
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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/
@@ -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
@@ -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";
@@ -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;
@@ -221,7 +229,8 @@ angular.module('ngCsv.directives').
addByteOrderMarker: "@addBom",
ngClick: '&',
charset: '@charset',
label: '&csvLabel'
label: '&csvLabel',
sortColumn: '@sortColumn'
},
controller: [
'$scope',
@@ -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 : ",";

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.

2,851 changes: 0 additions & 2,851 deletions npm-debug.log

This file was deleted.

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

7 changes: 6 additions & 1 deletion src/ng-csv/services/csv-service.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
* 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";
@@ -96,6 +96,11 @@ 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;
55 changes: 53 additions & 2 deletions test/unit/ngCsv/directives/ngCsv.js
Original file line number Diff line number Diff line change
@@ -134,14 +134,14 @@ describe('ngCsv directive', function () {
$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');
@@ -532,4 +532,55 @@ describe('ngCsv directive', function () {

scope.$apply();
});

it('Accepts optional sortColumn attribute (input string) with ascending sort', function (done) {
$rootScope.sortColumn = "-age";
$rootScope.jsondata = [{name:'John', phone:'555-1212', age:10},
{name:'Mary', phone:'555-9876', age:19},
{name:'Mike', phone:'555-4321', age:21},
{name:'Adam', phone:'555-5678', age:35},
{name:'Julie', phone:'555-8765', age:29}];

var element = $compile(
'<div ng-csv="jsondata" sort-column="{{sortColumn}}" filename="custom.csv"></div>')($rootScope);

$rootScope.$digest();

var scope = element.isolateScope();

expect(scope.$eval(scope.data)).toEqual($rootScope.jsondata);
expect(scope.sortColumn).toEqual($rootScope.sortColumn);

scope.buildCSV(scope.data).then(function() {
expect(scope.csv).toBe('Adam,555-5678,35\r\nJulie,555-8765,29\r\nMike,555-4321,21\r\nMary,555-9876,19\r\nJohn,555-1212,10\r\n');
done();
});
scope.$apply();
});

it('Accepts optional sortColumn attribute (input string) with descending sort', function (done) {
$rootScope.sortColumn = "age";
$rootScope.jsondata = [{name:'John', phone:'555-1212', age:10},
{name:'Mary', phone:'555-9876', age:19},
{name:'Mike', phone:'555-4321', age:21},
{name:'Adam', phone:'555-5678', age:35},
{name:'Julie', phone:'555-8765', age:29}];

var element = $compile(
'<div ng-csv="jsondata" sort-column="{{sortColumn}}" filename="custom.csv"></div>')($rootScope);

$rootScope.$digest();

var scope = element.isolateScope();

expect(scope.$eval(scope.data)).toEqual($rootScope.jsondata);
expect(scope.sortColumn).toEqual($rootScope.sortColumn);

scope.buildCSV(scope.data).then(function() {
expect(scope.csv).toBe('John,555-1212,10\r\nMary,555-9876,19\r\nMike,555-4321,21\r\nJulie,555-8765,29\r\nAdam,555-5678,35\r\n');
done();
});
scope.$apply();
});

});