在AngularJS项目中,我使用智能表模块和asafdav/ng-csv将数据从智能表导出到CSV。
我成功地将数据从智能表导出到csv,但仅从第一页导出。我从$scope.displayedCollection变量导出数据(与st-table指令中的相同)。在此变量中,数据与表中的数据完全相同,但仅来自第一页。您知道如何导出整个数据(通过从智能表中进行排序和筛选)。我认为,在将数据拆分为页面之前,我应该“插入”到智能表模块。但是怎么做呢?
发布于 2015-09-01 07:49:09
我创建了自己的指令,用于在分页之前访问表中的数据:
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();
});
}
};
});要使用它,请添加带有变量名的st-filtered-collection属性,该属性将在分页前设置为表中的数据:
<table st-table="..." st-safe-src="..." st-filtered-collection="filteredCollection">
...
</table>从现在开始,在控制器中您可以使用$scope.filteredCollection变量。
发布于 2019-10-09 22:18:01
将ng-csv.min.js添加到主文件(index.html)中。还请确保您正在添加angular-sanitize.min.js。
HTML:
<td><a class="btn btn-default" st-expor ng-csv="displayed" filename="test.csv" csv-header="['Field A', 'Field B', 'Field C']">ExportNow</a></td>主计长:
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);
}
}
}
})https://stackoverflow.com/questions/32175813
复制相似问题