-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example with export to csv and update readme
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!DOCTYPE html> | ||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | ||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | ||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | ||
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | ||
<meta name="viewport" content="width=device-width"> | ||
|
||
<link rel="stylesheet" href="css/bootstrap.min.css"> | ||
<link rel="stylesheet" href="css/bootstrap-theme.min.css"> | ||
<script src="js/angular.min.js"></script> | ||
<script src="../ng-table.src.js"></script> | ||
<script src="js/ng-table-export.src.js"></script> | ||
<link rel="stylesheet" href="../ng-table.css"> | ||
</head> | ||
<body ng-app="main"> | ||
<h1>Export to CSV</h1> | ||
|
||
<div ng-controller="DemoCtrl"> | ||
|
||
<a class="btn btn-primary" ng-mousedown="csv.generate()" ng-href="{{ csv.link() }}" download="test.csv">Export to CSV</a> | ||
|
||
<table ng-table="tableParams" class="table" export-csv="csv"> | ||
<tr ng-repeat="user in $data"> | ||
<td data-title="'Name'"> | ||
{{user.name}} | ||
</td> | ||
<td data-title="'Age'"> | ||
{{user.age}} | ||
</td> | ||
</tr> | ||
</table> | ||
|
||
<script> | ||
var app = angular.module('main', ['ngTable', 'ngTableExport']). | ||
controller('DemoCtrl', function($scope, ngTableParams, $sce) { | ||
var data = [{name: "Moroni", age: 50}, | ||
{name: "Tiancum", age: 43}, | ||
{name: "Jacob", age: 27}, | ||
{name: "Nephi", age: 29}, | ||
{name: "Enos", age: 34}, | ||
{name: "Tiancum", age: 43}, | ||
{name: "Jacob", age: 27}, | ||
{name: "Nephi", age: 29}, | ||
{name: "Enos", age: 34}, | ||
{name: "Tiancum", age: 43}, | ||
{name: "Jacob", age: 27}, | ||
{name: "Nephi", age: 29}, | ||
{name: "Enos", age: 34}, | ||
{name: "Tiancum", age: 43}, | ||
{name: "Jacob", age: 27}, | ||
{name: "Nephi", age: 29}, | ||
{name: "Enos", age: 34}]; | ||
|
||
$scope.tableParams = new ngTableParams({ | ||
page: 1, // show first page | ||
count: 10 // count per page | ||
}, { | ||
total: data.length, // length of data | ||
getData: function($defer, params) { | ||
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count())); | ||
} | ||
}); | ||
}); | ||
</script> | ||
|
||
</div> | ||
|
||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
angular.module('ngTableExport', []) | ||
.config(['$compileProvider', function($compileProvider) { | ||
// allow data links | ||
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/); | ||
}]) | ||
.directive('exportCsv', ['$parse', function ($parse) { | ||
return { | ||
restrict: 'A', | ||
scope: false, | ||
link: function(scope, element, attrs) { | ||
var data = ''; | ||
var csv = { | ||
stringify: function(str) { | ||
return '"' + | ||
str.replace(/^\s\s*/, '').replace(/\s*\s$/, '') // trim spaces | ||
.replace(/"/g,'""') + // replace quotes with double quotes | ||
'"'; | ||
}, | ||
generate: function() { | ||
data = ''; | ||
var rows = element.find('tr'); | ||
angular.forEach(rows, function(row, i) { | ||
var tr = angular.element(row), | ||
tds = tr.find('th'), | ||
rowData = ''; | ||
if (tr.hasClass('ng-table-filters')) { | ||
return; | ||
} | ||
if (tds.length == 0) { | ||
tds = tr.find('td'); | ||
} | ||
if (i != 1) { | ||
angular.forEach(tds, function(td, i) { | ||
rowData += csv.stringify(angular.element(td).text()) + ';'; | ||
}); | ||
rowData = rowData.slice(0, rowData.length - 1); //remove last semicolon | ||
} | ||
data += rowData + "\n"; | ||
}); | ||
}, | ||
link: function() { | ||
return 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data); | ||
} | ||
}; | ||
$parse(attrs.exportCsv).assign(scope.$parent, csv); | ||
} | ||
}; | ||
}]); |