将dojo datagrid导出到csv文件 [英] Exporting a dojo datagrid to a csv file
问题描述
我正在寻找一个javascript函数,它会将我的datagrid(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..
谢谢
Andy
推荐答案
我使用带有EnhancedGrid的Exporter插件,使用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.
PS:建议仅将iFrame用于内部/内部Web应用程序。
PS: It is recommended to use iFrame only for internal/in-house web applications.
这篇关于将dojo datagrid导出到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!