Export data from Smart Table in AngularJS
在AngularJS项目中,我使用Smart Table模块和asafdav / ng-csv将数据从智能表导出到CSV。
我成功地将数据从智能表导出到csv,但仅从首页导出。 我从
我创建了自己的指令以在分页之前从表访问数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | angular.module('smart-table') .directive('stFilteredCollection', function () { return { restrict: 'A', require: '^stTable', scope: { stFilteredCollection: '=' }, controller: 'stTableController', link: function (scope, element, attr, ctrl) { scope.$watch(function () { return ctrl.getFilteredCollection(); }, function (newValue, oldValue) { scope.stFilteredCollection = ctrl.getFilteredCollection(); }); } }; }); |
要使用它,请添加带有变量名称的
1 2 3 | <table st-table="..." st-safe-src="..." st-filtered-collection="filteredCollection"> ... </table> |
从现在开始,您可以在控制器中使用
将
HTML:
1 | <td>ExportNow</td> |
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | top line var myApp = angular.module('myApp', ['smart-table', 'ui.bootstrap','ngSanitize', 'ngCsv']); somewhere in your controller .directive('stExport', function(csv){ return { restrict:'E', require:'^stTable', template:'<button ng-click="export()"></button>', link:function(scope, element, smartTable){ scope.export = function(){ var filtered = smartTable.getFilteredCollection(); csv(filtered); } } } }) |