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

feat(skip-empty) added new option to skip downloading empty CSV #131

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ ngCsv attributes
* 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
* quote-strings: If provided, will force escaping of every string field.
* skip-empty: If provided, will not trigger the download of empty data (default is false).
* 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".
Expand Down
7 changes: 6 additions & 1 deletion build/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ angular.module('ngCsv.directives').
addByteOrderMarker: "@addBom",
ngClick: '&',
charset: '@charset',
label: '&csvLabel'
label: '&csvLabel',
skipEmpty: '@skipEmpty'
},
controller: [
'$scope',
Expand Down Expand Up @@ -284,6 +285,10 @@ angular.module('ngCsv.directives').
],
link: function (scope, element, attrs) {
function doClick() {

// drop out for empty data
if (!scope.csv.trim() && scope.skipEmpty === "true") {return;}

var charset = scope.charset || "utf-8";
var blob = new Blob([scope.csv], {
type: "text/csv;charset="+ charset + ";"
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.

7 changes: 6 additions & 1 deletion src/ng-csv/directives/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ angular.module('ngCsv.directives').
addByteOrderMarker: "@addBom",
ngClick: '&',
charset: '@charset',
label: '&csvLabel'
label: '&csvLabel',
skipEmpty: '@skipEmpty'
},
controller: [
'$scope',
Expand Down Expand Up @@ -84,6 +85,10 @@ angular.module('ngCsv.directives').
],
link: function (scope, element, attrs) {
function doClick() {

// drop out for empty data
if (!scope.csv.trim() && scope.skipEmpty === "true") {return;}

var charset = scope.charset || "utf-8";
var blob = new Blob([scope.csv], {
type: "text/csv;charset="+ charset + ";"
Expand Down
27 changes: 25 additions & 2 deletions test/unit/ngCsv/directives/ngCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -192,6 +192,29 @@ describe('ngCsv directive', function () {
scope.$apply();
});

it('Accepts optional skip-empty attribute', function () {

$rootScope.noData = [];
var element = $compile(
'<div ng-csv="noData" skip-empty="true" ' +
' filename="no-download.csv">' +
'</div>')($rootScope);

$rootScope.$digest();

var scope = element.isolateScope();

spyOn(scope, 'buildCSV');

// Check that the compiled element contains the templated content
expect(scope.$eval(scope.data)).toEqual($rootScope.noData);
expect(scope.skipEmpty).toBe('true');

scope.$apply();

expect(scope.buildCSV).not.toHaveBeenCalled();
});

it('Accepts optional text-delimiter attribute (input object)', function (done) {
$rootScope.testDelim = [{a: 1, b: 'a', c: 2}, {a: 'b', b: 'c', c: 3}];
var element = $compile(
Expand Down