From 32a220acf60f0bcefe637999d5f16587ad993b7f Mon Sep 17 00:00:00 2001
From: legac <renaud.legac@free.fr>
Date: Sat, 12 Jul 2014 13:11:36 +0200
Subject: [PATCH] the method Export.toCSV is working.

---
 static/plugin_dbui/src/grid/plugin/Export.js | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/static/plugin_dbui/src/grid/plugin/Export.js b/static/plugin_dbui/src/grid/plugin/Export.js
index e7c779d2..d660ca85 100644
--- a/static/plugin_dbui/src/grid/plugin/Export.js
+++ b/static/plugin_dbui/src/grid/plugin/Export.js
@@ -9,6 +9,7 @@ Ext.define('App.grid.plugin.Export', {
     alias: 'plugin.pGridExport',
 
     delimiter: '"',
+    newLine: '\n',
     separator: ',',
 
     // Pre-defined configuration
@@ -44,6 +45,7 @@ Ext.define('App.grid.plugin.Export', {
      * Export the content of the grid as a CSV file.
      * The method scan the HTML to find the rendered headers, rows
      * and cells content.
+     * Hidden columns and rows are ignored.
      */
     toCSV: function () {
 
@@ -56,7 +58,9 @@ Ext.define('App.grid.plugin.Export', {
             cell,
             columns,
             i,
+            link,
             row,
+            s,
             value;
 
         // scan the grid header
@@ -105,6 +109,16 @@ Ext.define('App.grid.plugin.Export', {
             row = row.next();
         } while (row);
 
-        console.log(csv.join('\n'));
+        // special character like quote, / , ? are HTML encoded
+        s = encodeURIComponent(csv.join(this.newLine));
+
+        // download the file by simulating a click on a link
+        link = Ext.getBody().createChild({
+            tag: "a",
+            href: "data:text/csv;charset=utf-8," + s,
+            download: "my_data.csv"
+        });
+        link.dom.click();
+        link.destroy();
     }
 });
\ No newline at end of file
-- 
GitLab