Skip to content

Commit 57be8f7

Browse files
committed
Refactored the directive and updated the docs accordingly
1 parent bafe2fe commit 57be8f7

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ An AngularJS simple directive that turns arrays and objects into downloadable CS
1717
<button type="button" ng-csv="getArray()" filename="test.csv">Export</button>
1818
```
1919

20-
##### New! - Now accepts a header row as parameter
21-
Just provide csv-header attribute.
22-
```html
20+
ngCsv attributes
21+
----------------
22+
* ng-csv: The data array
23+
* filename: The filename that will be stored on the user's computer
24+
* csv-header: If provided, would use this attribute to create a header line
25+
```html
2326
<button type="button" ng-csv="getArray()" csv-header="['Field A', 'Field B', 'Field C']" filename="test.csv">Export</button>
2427
```
28+
* field-separator: Defines the field separator character (default is)
29+
* text-delimiter: If provided, will use this characters to "escape" string values
2530

2631

2732
## Example

build/ng-csv.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ angular.module('ngCsv.directives', []).
4848
$scope, $element, $attrs, $transclude
4949
) {
5050
$scope.csv = '';
51-
$scope.$watch($scope.data, function (newValue) {
52-
$scope.buildCsv(newValue);
53-
}, true);
5451

55-
$scope.$watch('fieldSep', function() {
56-
$scope.buildCsv($scope.data());
57-
});
58-
59-
$scope.$watch('txtDelim', function() {
60-
$scope.buildCsv($scope.data());
61-
});
52+
$scope.$watch(function (newValue) {
53+
$scope.buildCsv();
54+
}, true);
6255

63-
$scope.buildCsv = function (data) {
56+
$scope.buildCsv = function () {
57+
var data = $scope.data();
6458
var csvContent = 'data:text/csv;charset=utf-8,';
6559

6660
// Check if there's a provided header array
@@ -89,6 +83,7 @@ angular.module('ngCsv.directives', []).
8983
});
9084

9185
$scope.csv = encodeURI(csvContent);
86+
return $scope.csv;
9287
};
9388

9489
$scope.getFilename = function () {

build/ng-csv.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ <h1>ngCsv <small>example</small></h1>
2222
<input type="text" id="filename" class="form-control" ng-model="filename">
2323
</div>
2424

25-
<button class="btn btn-default" ng-csv="getArray" filename="{{ filename }}.csv">Export to CSV</button>
26-
<button class="btn btn-default" ng-csv="getArray" csv-header="getHeader()" filename="{{ filename }}">Export to CSV with header</button>
25+
<div class="form-group">
26+
<label for="separator">Field separator</label>
27+
<input type="text" id="separator" class="form-control" ng-model="separator" ng-init="separator=','">
28+
</div>
29+
30+
<button class="btn btn-default"
31+
ng-csv="getArray" filename="{{ filename }}.csv" field-separator="{{separator}}"
32+
on-demand="{{onDemand}}">Export to CSV</button>
33+
<button class="btn btn-default"
34+
ng-csv="getArray" csv-header="getHeader()" filename="{{ filename }}" field-separator="{{separator}}"
35+
on-demand="{{onDemand}}">Export to CSV with header</button>
36+
37+
<button class="btn btn-default" ng-click="addRandomRow()">Add row</button>
2738
</div>
2839
</div>
2940

@@ -33,6 +44,10 @@ <h1>ngCsv <small>example</small></h1>
3344
myapp.controller('myctrl', function ($scope) {
3445
$scope.filename = "test";
3546
$scope.getArray = [{a: 1, b:2}, {a:3, b:4}];
47+
48+
$scope.addRandomRow = function() {
49+
$scope.getArray.push({a: Math.floor((Math.random()*10)+1), b: Math.floor((Math.random()*10)+1)});
50+
}
3651
$scope.getHeader = function () {return ["A", "B"]};
3752
});
3853
</script>

src/ng-csv/directives/ng-csv.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ angular.module('ngCsv.directives', []).
2121
$scope, $element, $attrs, $transclude
2222
) {
2323
$scope.csv = '';
24-
$scope.$watch($scope.data, function (newValue) {
25-
$scope.buildCsv(newValue);
26-
}, true);
2724

28-
$scope.$watch('fieldSep', function() {
29-
$scope.buildCsv($scope.data());
30-
});
31-
32-
$scope.$watch('txtDelim', function() {
33-
$scope.buildCsv($scope.data());
34-
});
25+
$scope.$watch(function (newValue) {
26+
$scope.buildCsv();
27+
}, true);
3528

36-
$scope.buildCsv = function (data) {
29+
$scope.buildCsv = function () {
30+
var data = $scope.data();
3731
var csvContent = 'data:text/csv;charset=utf-8,';
3832

3933
// Check if there's a provided header array
@@ -62,6 +56,7 @@ angular.module('ngCsv.directives', []).
6256
});
6357

6458
$scope.csv = encodeURI(csvContent);
59+
return $scope.csv;
6560
};
6661

6762
$scope.getFilename = function () {

0 commit comments

Comments
 (0)