阿里云新用户优惠

二十五、UI-Grid Exporting Data 导出数据

原文:206 Exporting Data

导出功能允许将数据从表格中导出为 csv 或 pdf 格式,可以导出所有数据、可见数据或所选数据。

要使用导出功能, 需要在表格上引入 ui-grid-exporter指令。如果要导出选定的行, 则必须在表格上引入 ui-grid-selection指令。如果要导出为 pdf, 您需要安装 pdfMake, 可通过以下操作: bower install pdfmake

ui.grid.exporter的api可以在 下列中使用:

  • columnDef
  • gridOptions
  • publicApi

导出选项在表格的菜单栏中, 可以在gridOption 中通过enableGridMenu来启用表格菜单。 请注意, 如果选择了数据, 则仅显示导出所选数据的选项。

如果您使用 internet explorer, 那么 pdf 将自动下载而不是打开, 有问题与 pdfMake 无法自动打开选项卡在 ie 浏览器(请参阅该地址中的其他问题。 https://github.com/bpampuch/pdfmake/issues/16

一些早期版本的 excel 不喜欢 utf-16 bom–第一个表示 csv 文件包含 utf-16 内容的少数字符。大多数其他应用程序和较新的 excel 版本都可以成功地使用它。如果excel 中的前几个字符有乱码的问题, 可以尝试设置 exporterOlderExcelCompatibility: true

你可能会发现这个问题, 当 pdfMake 在压缩时, 可能会发生错误。如果您有这个问题, 建议改为包括 pdfmake. js 和 vfs_fonts. 作为素材(或等效的素材版本), 然后将它们作为脚本包含在应用程序中, 而不是作为整个 javascript 库的一部分。

例子 在本示例中, 我们使用表格菜单按钮, 同时显示 pdf 和 csv 导出选项。

  • index.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>
    <script src="http://ui-grid.info/release/ui-grid.css"></script>
    <script src="app.js"></script>
  </head>
  <body>
    <div ng-controller="MainCtrl">
      <div ui-grid="gridOptions" ui-grid-selection ui-grid-exporter class="grid"></div>
    </div>
  </body>
</html>
  • main.css
.grid {
  width: 500px;
  height: 400px;
}
  • app.js
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},
    exporterPdfTableStyle: {margin: [30, 30, 30, 30]},
    exporterPdfTableHeaderStyle: {fontSize: 10, bold: true, italics: true, color: 'red'},
    exporterPdfHeader: { text: "My Header", style: 'headerStyle' },
    exporterPdfFooter: function ( currentPage, pageCount ) {
      return { text: currentPage.toString() + ' of ' + pageCount.toString(), style: 'footerStyle' };
    },
    exporterPdfCustomFormatter: function ( docDefinition ) {
      docDefinition.styles.headerStyle = { fontSize: 22, bold: true };
      docDefinition.styles.footerStyle = { fontSize: 10, bold: true };
      return docDefinition;
    },
    exporterPdfOrientation: 'portrait',
    exporterPdfPageSize: 'LETTER',
    exporterPdfMaxGridWidth: 500,
    exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
    onRegisterApi: function(gridApi){
      $scope.gridApi = gridApi;
    }
  };

  $http.get('http://ui-grid.info/data/100.json')
  .success(function(data) {
    $scope.gridOptions.data = data;
  });

}]);

关注微信公众号,与我交流吧~

分享