javascript AngularJS ng-grid 为 selectedItems 导出到 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19263445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:55:26  来源:igfitidea点击:

AngularJS ng-grid Export to Excel for selectedItems

javascriptjqueryangularjsexport-to-excelng-grid

提问by Ayan

We have a ng-grid which displays data retrieved from a rest service. I want to export the rows selected by user(ng-grid selection check box enabled) to an excel and open that excel. The current behavior of app is Export to excel button calling a rest service with selected items from ng-grid and using POI to put the data into excel and returning the excel.

我们有一个 ng-grid,它显示从休息服务中检索到的数据。我想将用户选择的行(启用 ng-grid 选择复选框)导出到 excel 并打开该 excel。应用程序的当前行为是“导出到 excel”按钮,使用 ng-grid 中的选定项目调用休息服务,并使用 POI 将数据放入 excel 并返回 excel。

Is there a better way to do this? The data is already in browser, is there any angularjs, jQuery or javascript function to do this?

有一个更好的方法吗?数据已经在浏览器中,是否有任何 angularjs、jQuery 或 javascript 函数来执行此操作?

回答by AardVark71

There is a plugin for csv export : https://github.com/angular-ui/ng-grid/tree/master/plugins

有一个 csv 导出插件:https: //github.com/angular-ui/ng-grid/tree/master/plugins

It can be easily added by adding it to your gridoptions, while making sure to display the footer.

可以通过将它添加到您的 gridoptions 来轻松添加它,同时确保显示页脚。

 $scope.gridOptions = {
        data: ...,
        plugins: [new ngGridCsvExportPlugin()],
        showFooter: true,
        ...
      };

It exports all your records from your specified grid.data.

它从您指定的 grid.data 导出您的所有记录。

[Although you can pass in an options object for specifying columns etc. there isn't an option currently to limit to selectedItems.]

[虽然你可以传入一个选项对象来指定列等。目前没有一个选项来限制 selectedItems。]

It looks easy though to adjust the ng-grid-csv-export.js source to loop over grid.mySelections instead over grid.data. (not tested by me yet)

虽然调整 ng-grid-csv-export.js 源代码以在 grid.mySelections 上循环而不是在 grid.data 上循环,但看起来很容易。(我还没有测试过)