2010-12-14 6 views
1

내 데이터 격자 (zero.grid.DataGrid)를 CSV 파일이나 유사한 스프레드 시트 응용 프로그램에서 열 수있는 데이터로 내보내는 javascript 함수를 얻으려고합니다.dojo 데이터 격자를 csv 파일로 내보내기

가 이것을 수행하는 표준 방법이 있나요 ..

감사

앤디

답변

3

나는 EnhancedGrid가 백엔드로 서블릿을 사용하여 내보내기 플러그인을 사용하여 힘든 시간을 보냈습니다. 마지막으로 나는 그것이 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> 

.

Export Data from Grid in Dojo

PS : 그것은 단지 내부/사내 웹 애플리케이션에 iframe을 사용하는 것이 좋습니다.

+0

백엔드에서 서블릿 코드를 제공 할 수 있습니까? 백엔드에서 어떻게 처리됩니까? –

5

이렇게하면 PHP 서버 측 스크립트를 사용하는 것을 달성 할 방법이다.

  grid.exportGrid("csv",{ 
      writerArgs: { 
        separator: "," 
        } 
      }, function(str){ 
        var form = document.createElement('form'); 
        dojo.attr(form, 'method', 'POST'); 
        document.body.appendChild(form); 
        dojo.io.iframe.send({ 
          url: "CSVexport.php", 
          form: form, 
          method: "POST", 
          content: {exp: str}, 
          timeout: 15000 
        }); 
        document.body.removeChild(form); 
        } 
      ); 

CSVexport.php :

<? 

    $time = time(); 
    header("Pragma: public"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-type: application/csv"); 
    header("Content-Disposition: attachment; filename=\"grid_$time.csv\""); 
    $exportedData = $_POST['exp']; 
    echo $exportedData; 

    ?> 
0
<!DOCTYPE HTML> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>CISROMM - Master Milestone List Editor</title> 
     <!-- Include dojo dependencies --> 
     <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8/dojo/resources/dojo.css"> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8/dijit/themes/tundra/tundra.css"> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8/dijit/themes/claro/claro.css"> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8/dijit/themes/claro/document.css"> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8/dojox/grid/enhanced/resources/claro/EnhancedGrid.css"> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css"> 
     <script src='http://ajax.googleapis.com/ajax/libs/dojo/1.8/dojo/dojo.js'></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} 
           });*/ 
           //alert(str); 
           document.getElementById("output").value=str; 

          }); 
       }; 
     <!-- Data Export Handler --> 


     var grid, store; 
     <!-- Grid Creation --> 
     dojo.ready(function(){ 
       data = { 
       identifier: 'id', 
       label: 'id', 
       items: [] 
      }; 
      data_list = [ 
       { col1: "normal", col2: false, col3: "new", col4: 'But are not followed by two hexadecimal', col5: 29.91, col6: 10, col7: false }, 
       { col1: "important", col2: false, col3: "new", col4: 'Because a % sign always indicates', col5: 9.33, col6: -5, col7: false }, 
       { col1: "important", col2: false, col3: "read", col4: 'Signs can be selectively', col5: 19.34, col6: 0, col7: true }, 
       { col1: "note", col2: false, col3: "read", col4: 'However the reserved characters', col5: 15.63, col6: 0, col7: true }, 
       { col1: "normal", col2: false, col3: "replied", col4: 'It is therefore necessary', col5: 24.22, col6: 5.50, col7: true }, 
       { col1: "important", col2: false, col3: "replied", col4: 'To problems of corruption by', col5: 9.12, col6: -3, col7: true }, 
       { col1: "note", col2: false, col3: "replied", col4: 'Which would simply be awkward in', col5: 12.15, col6: -4, col7: false } 
      ]; 
      var rows = 100; 
      for(var i=0, l=data_list.length; i<rows; i++){ 
       data.items.push(dojo.mixin({ id: i }, data_list[i%l])); 
      } 

     // global var "test_store" 
     store = new dojo.data.ItemFileWriteStore({data: data}); 



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

       grid.startup(); 

     }); 
     </script> 

    </head> 
    <body class="claro"> 
     <button id="exportBtn" onClick="exportAll();"> 
           Export to Excel 
     </button> 

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

    </body> 
</html>