关于csv:从AngularJS中的智能表导出数据

Export data from Smart Table in AngularJS

在AngularJS项目中,我使用Smart Table模块和asafdav / ng-csv将数据从智能表导出到CSV。

我成功地将数据从智能表导出到csv,但仅从首页导出。 我从$scope.displayedCollection变量导出数据(与st-table指令相同)。 该变量与表中的数据完全相同,但仅来自第一页。 您知道我如何导出整个数据(通过智能表进行排序和过滤)。 我认为在将数据拆分为页面之前,我应该"插入"智能表模块。 但是如何?


我创建了自己的指令以在分页之前从表访问数据:

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();
        });
      }
    };
  });

要使用它,请添加带有变量名称的st-filtered-collection属性,该属性将在分页之前设置为表中的数据:

1
2
3
<table st-table="..." st-safe-src="..." st-filtered-collection="filteredCollection">
   ...
</table>

从现在开始,您可以在控制器中使用$scope.filteredCollection变量。


ng-csv.min.js添加到主文件(index.html)。 另请确保您要添加angular-sanitize.min.js

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);
              }
          }
        }
     })