从AngularJS中的智能表导出数据

时间:2015-08-24 06:35:23

标签: angularjs csv smart-table

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

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

2 个答案:

答案 0 :(得分:1)

我创建了自己的指令,用于在分页前访问表中的数据:

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变量。

答案 1 :(得分:0)

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