将 dojo 数据网格导出到 CSV 文件

2022-01-18 00:00:00 widget export-to-csv javascript dojo

我正在寻找一个 Javascript 函数,它将我的数据网格 (zero.grid.DataGrid) 中的数据导出到一个 CSV 文件或可以由电子表格应用程序打开的类似文件中.

I am looking to get a Javascript function which will export my datagrid (zero.grid.DataGrid) full of data into a CSV file or something similar which can be opened by a spreadsheet application.

有没有标准的方法可以做到这一点..

Is there any standard way of doing this out there..

推荐答案

我很难将 Exporter 插件与 EnhancedGrid 一起使用,并使用 servlet 作为后端.最后我使用 iFrame 使它工作:

I had tough time with using Exporter plugin with EnhancedGrid using servlet as backend. Finally I made it work by using iFrame:

<!DOCTYPE HTML>
<html lang="en">
   <head>
    <meta charset="utf-8">
    <title>CISROMM - Master Milestone List Editor</title>
    <!-- Include dojo dependencies -->
    <link rel="stylesheet" href="js/dojoroot/dojo/resources/dojo.css">
    <link rel="stylesheet" href="js/dojoroot/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="js/dojoroot/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css">
    <link rel="stylesheet" href="js/dojoroot/dojox/grid/enhanced/resources/EnhancedGrid.css">
    <link rel="stylesheet" href="js/dojoroot/dojox/grid/resources/claroGrid.css">
    <script src="js/dojoroot/dojo/dojo.js"
        data-dojo-config="isDebug: true,parseOnLoad: true">
    </script>
    <!-- Include dojo dependencies -->

    <!-- Require the widgets -->
    <script>
        dojo.require("dojox.grid.EnhancedGrid");
        dojo.require("dojo.data.ItemFileWriteStore");
        dojo.require("dojox.grid.enhanced.plugins.exporter.CSVWriter");
                dojo.require("dojo.io.iframe");
    <!-- Require the widgets -->                 

    <!-- Data Export Handler -->
        function exportAll(){
            dijit.byId("grid").exportGrid("csv", function(str){
                    dojo.io.iframe.create('exportFrame', 'exportFrameLoaded()', '');
                    dojo.io.iframe._currentDfd = null;
                    dojo.io.iframe.send({
                            url: "/ExportGrid.json",
                            content:{data:str}
                    });
                });
        };
    <!-- Data Export Handler -->

    var grid, store;
    <!-- Grid Creation -->
    dojo.ready(function(){

        store = new dojo.data.ItemFileWriteStore({ url: 'PopulateMsListEditor.json', urlPreventCache: 'yes', clearOnClose: 'yes'    });

        grid = new dojox.grid.EnhancedGrid({
            store: store,
            rowSelector: 'auto',
            query: {id: "*"} ,
            plugins: {
                  exporter: true
            },
            structure: [
                  {field: 'msConstId', width: '20%', name: 'Milestone',hidden: true},
                              {field: 'name', width: '20%', name: 'Milestone',editable: true}
            ]
        },"grid");

        grid.startup();

    });
    </script>

   </head>
   <body class="claro">
    <button id="exportBtn" data-dojo-type="dijit.form.Button"
            data-dojo-props="
                iconClass:'dijitIconFile',
                showLabel:true,
                onClick:function() {
                    exportAll();
                }">
                Export to Excel
    </button>

    <div id="grid" style="width: 560px;height: 680px;"></div>

   </body>
</html>

我写了一篇博文,这样我就不必再搜索了.

I have written a blog post so that I don't have to search again.

从 Dojo 中的网格导出数据

PS:建议仅将 iFrame 用于内部/内部 Web 应用程序.

PS: It is recommended to use iFrame only for internal/in-house web applications.

相关文章