Skip to content

Commit 1f4eb5f

Browse files
export to csv option add
1 parent 85a6f95 commit 1f4eb5f

1 file changed

Lines changed: 77 additions & 17 deletions

File tree

export/LightPivotTable.xml

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<Class name="DeepSee.LightPivotTable">
1313
<Super>%DeepSee.Component.Portlet.abstractPortlet</Super>
14-
<TimeChanged>63531,82258.204784</TimeChanged>
14+
<TimeChanged>63544,74499.681602</TimeChanged>
1515
<TimeCreated>63515,61322.546099</TimeCreated>
1616

1717
<Parameter name="INCLUDEFILES">
@@ -22,6 +22,10 @@
2222
<Type>%String</Type>
2323
</Property>
2424

25+
<Property name="ExportCSV">
26+
<Type>%Boolean</Type>
27+
</Property>
28+
2529
<Property name="ShowSummary">
2630
<Type>%Boolean</Type>
2731
</Property>
@@ -48,7 +52,8 @@
4852
kill pInfo // $LB(name,value,type,caption,title)
4953
5054
set pInfo($I(pInfo)) = $LB("DataSource", "/" _ $NAMESPACE, "%String", $$$Text("MDX2JSON source", "%DeepSee"), "Set the URL of MDX2JSON source. Example: ""/SAMPLES""")
51-
set pInfo($I(pInfo)) = $LB("ShowSummary", 1, "%Boolean", $$$Text("Show summary?", "%DeepSee"), "Show summary row")
55+
set pInfo($I(pInfo)) = $LB("ShowSummary", 1, "%Boolean", $$$Text("Show summary", "%DeepSee"), "Show summary row")
56+
set pInfo($I(pInfo)) = $LB("ExportCSV", 1, "%Boolean", $$$Text("Export to CSV", "%DeepSee"), "Show export to CSV format button")
5257
5358
quit $$$OK
5459
]]></Implementation>
@@ -105,13 +110,45 @@
105110
<ClientMethod>1</ClientMethod>
106111
<Implementation><![CDATA[
107112
var container = document.getElementById(this.id),
108-
controller,
113+
basicContainer = container,
114+
self = this;
115+
116+
if (!container) {
117+
console.error("Light pivot table: Unable to get widget with ID=" + this.id);
118+
setTimeout(function () { self.onCreate.call(self); }, 1);
119+
return;
120+
}
121+
122+
for (i in container.childNodes) {
123+
if (container.childNodes[i].className === "lpt-container") {
124+
container = container.childNodes[i];
125+
break;
126+
}
127+
}
128+
129+
var controller,
109130
source,
110131
setup,
111132
_ = this,
112133
widget = this, // It's lie. It changes later.
113134
widgetKey,
114-
i, info = {};
135+
i, info = {},
136+
exportToExcel = !!parseInt(container.getAttribute("export-csv"));
137+
138+
if (exportToExcel) {
139+
var widgetHead = document.getElementById(basicContainer.getAttribute("id").replace(/\/.*/, "/header"));
140+
if (widgetHead) {
141+
var f,
142+
widgetTr = (f = function (el) {var r; if (el.tagName === "TR") return el; for (var i in el.childNodes) { r = f(el.childNodes[i]); if (r) return r; } return null; })(widgetHead);
143+
if (widgetTr) {
144+
var td = document.createElement("TD");
145+
td.selfW = _;
146+
td.setAttribute("style", "padding-right:6px;vertical-align:bottom;");
147+
td.innerHTML = "<div class=\"zendiv\" onclick=\"this.parentNode.selfW.exportCSV.call(this.parentNode.selfW)\"><img class=\"imageLink\" src=\"deepsee/ds2_action_44.png\" title=\"Export to CSV format\"></div>";
148+
widgetTr.appendChild(td);
149+
}
150+
}
151+
}
115152
116153
var post = function (url, data, callback) {
117154
@@ -138,18 +175,6 @@
138175
139176
};
140177
141-
if (!container) {
142-
console.error("Light pivot table: Unable to get widget with ID=" + this.id);
143-
return;
144-
}
145-
146-
for (i in container.childNodes) {
147-
if (container.childNodes[i].className === "lpt-container") {
148-
container = container.childNodes[i];
149-
break;
150-
}
151-
}
152-
153178
setTimeout(function() { // onCreate fired when scripts are ready, but this.parent is missed.
154179
155180
controller = _.getConnectedController();
@@ -195,6 +220,41 @@
195220
]]></Implementation>
196221
</Method>
197222

223+
<Method name="exportCSV">
224+
<Description>
225+
Firing this method will cause CSV-file download</Description>
226+
<Language>javascript</Language>
227+
<ClientMethod>1</ClientMethod>
228+
<Implementation><![CDATA[
229+
if (!this.LightPivotTable) return;
230+
//console.log(this.LightPivotTable);
231+
var link = document.createElement("a"),
232+
csvData = [],
233+
rawData = this.LightPivotTable.dataController.getData(),
234+
row, cell;
235+
236+
if (!rawData || !(rawData.rawData instanceof Array)) return;
237+
rawData = rawData.rawData;
238+
239+
for (var y = 0; y < rawData.length; y++) {
240+
row = [];
241+
for (var x = 0; x < rawData[y].length; x++) {
242+
cell = (rawData[y][x].value || "").toString();
243+
if (isFinite(cell)) cell = cell.replace(/\./g, ",");
244+
if (cell.indexOf("\"") > -1 || cell.indexOf(";") > -1) {
245+
cell = "\"" + cell.replace(/"/g, "\"\"") + "\"";
246+
}
247+
row.push(cell);
248+
}
249+
csvData.push(row.join(";"));
250+
}
251+
252+
link.setAttribute("href", "data:application/csv;charset=UTF-8,\uFEFF"+encodeURIComponent(csvData.join("\r\n")));
253+
link.setAttribute("download", "EXPORT.csv");
254+
link.click();
255+
]]></Implementation>
256+
</Method>
257+
198258
<Method name="%DrawHTML">
199259
<Implementation><![CDATA[
200260
set ..renderFlag = ..renderFlag + 1
@@ -214,7 +274,7 @@
214274
}
215275
216276
&html<
217-
<div data-source="#(..DataSource)#" show-summary="#(..ShowSummary)#" class="lpt-container" style="position: absolute; left: 0; bottom: 0; width: 100%; height: 100%;">
277+
<div export-csv="#(..ExportCSV)#" data-source="#(..DataSource)#" show-summary="#(..ShowSummary)#" class="lpt-container" style="position: absolute; left: 0; bottom: 0; width: 100%; height: 100%;">
218278
219279
</div>
220280
>

0 commit comments

Comments
 (0)