javascript - Angular UI 网格 : export hidden columns when user selects Export Visible data

标签 javascript angularjs angular-ui-grid

嗨,我正在使用 Angular UI 网格。我有 3 列。其中一列被隐藏。当用户单击“将可见数据导出为 CSV”或“将可见数据导出为 Pdf”时,还需要导出隐藏列。我怎样才能实现这个目标?

这是 plunkr 的链接 http://plnkr.co/edit/Vwq7azXtx0GV7idvrSvq?p=preview

HTML: 这是我的代码

<!doctype html>
<html ng-app="app">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
    <link rel="stylesheet" href="main.css" type="text/css">
  </head>
  <body>

<div ng-controller="MainCtrl">
  <div ui-grid="gridOptions" ui-grid-selection ui-grid-exporter class="grid"></div>
</div>

JS

<script src="app.js"></script>
  </body>
</html> 
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.exporter']);

app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
  $scope.gridOptions = {
    columnDefs: [
      { field: 'name' },
      { field: 'gender', visible: false},
      { field: 'company' }
    ],
    enableGridMenu: true,
    enableSelectAll: true,
    exporterCsvFilename: 'myFile.csv',
    exporterPdfDefaultStyle: {fontSize: 9},
    exporterPdfOrientation: 'portrait',
    exporterPdfPageSize: 'LETTER',
    exporterPdfMaxGridWidth: 500,
    onRegisterApi: function(gridApi){
      $scope.gridApi = gridApi;
    }
  };

  $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json')
  .success(function(data) {
    $scope.gridOptions.data = data;
  });

}]);

此处性别列已隐藏。但是,当我单击“将可见数据导出为 csv/pdf”时,我也希望导出此隐藏列。

最佳答案

我有同样的要求,并且能够通过以下方式完成此任务。

$scope.gridOptions = {
    exporterMenuCsv: true,
    exporterMenuPdf: true,
    exporterMenuVisibleData: false, // suppress default menu options

    columnDefs: [
        // define columns
    ],

    onRegisterApi: function (gridApi) {
        const grid = gridApi.grid;

        // add our own export visible data menu option
        // we want to export all columns, not just visible ones
        // add a small delay because the addToGridMenu function
        // may not be defined yet when this code is executed
        $timeout( function() {
            grid.api.core.addToGridMenu(grid, [
                {
                    title: i18nService.getSafeText('gridMenu.exporterVisibleAsCsv'),
                    action: function ($event) {
                        grid.api.exporter.csvExport(uiGridExporterConstants.VISIBLE, uiGridExporterConstants.ALL);
                    },
                    shown: function () {
                        return grid.options.exporterMenuCsv;
                    },
                    order: grid.options.exporterMenuItemOrder + 1
                },
                {
                    title: i18nService.getSafeText('gridMenu.exporterVisibleAsPdf'),
                    action: function ($event) {
                        grid.api.exporter.pdfExport(uiGridExporterConstants.VISIBLE, uiGridExporterConstants.ALL);
                    },
                    shown: function () {
                        return grid.options.exporterMenuPdf;
                    },
                    order: grid.options.exporterMenuItemOrder + 4
                }
            ]);
        }, 200 );
    }
}

代码隐藏了 ui-grid-exporter 模块添加的默认“导出可见数据...”菜单选项并添加了新选项。操作函数调用导出方法,为列类型指定 uiGridExporterConstants.ALL 而不是 uiGridExporterConstants.VISIBLE。

关于javascript - Angular UI 网格 : export hidden columns when user selects Export Visible data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41699269/

相关文章:

angularjs - Angular ui 网格工具提示不起作用

css - Angular UI-Grid Awesome Bootstrap 复选框无法正常运行

javascript - Babel Decorators 转换,无论我尝试什么,都会不断获得意想不到的标记(使用 React 项目)

javascript - 更改页面滚动上的事件导航图像?

javascript - ESlint 编译 es6 导入/导出语句失败

angularjs - 使用 NginX 提供 Angular 应用程序

javascript - AngularJS 中 $interval 返回的 Promise 的参数是什么?

javascript - 为什么命名为 : false does not work for me

javascript - 记录不同的数组

angularjs - 正确清除整个 AngularJS ui-grid 图表