forked from edspencer/Ext.ux.Exporter
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Exporter.js
82 lines (68 loc) · 2.69 KB
/
Exporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @class Ext.ux.Exporter
* @author Ed Spencer (http://edspencer.net), with modifications from iwiznia.
* Class providing a common way of downloading data in .xls or .csv format
*/
Ext.define("Ext.ux.exporter.Exporter", {
uses: [
"Ext.ux.exporter.Base64",
"Ext.ux.exporter.Button",
"Ext.ux.exporter.csvFormatter.CsvFormatter",
"Ext.ux.exporter.excelFormatter.ExcelFormatter"
],
statics: {
exportAny: function(component, formatter, config) {
var func = "export";
if(!component.is) {
func = func + "Store";
} else if(component.is("gridpanel")) {
func = func + "Grid";
} else if (component.is("treepanel")) {
func = func + "Tree";
} else {
func = func + "Store";
component = component.getStore();
}
return this[func](component, this.getFormatterByName(formatter), config);
},
/**
* Exports a grid, using the .xls formatter by default
* @param {Ext.grid.GridPanel} grid The grid to export from
* @param {Object} config Optional config settings for the formatter
*/
exportGrid: function(grid, formatter, config) {
config = config || {};
formatter = this.getFormatterByName(formatter);
var columns = Ext.Array.filter(grid.columns, function(col) {
return !col.hidden; // && (!col.xtype || col.xtype != "actioncolumn");
});
Ext.applyIf(config, {
title : grid.title,
columns: columns
});
return formatter.format(grid.store, config);
},
exportStore: function(store, formatter, config) {
config = config || {};
formatter = this.getFormatterByName(formatter);
Ext.applyIf(config, {
columns: store.fields ? store.fields.items : store.model.prototype.fields.items
});
return formatter.format(store, config);
},
exportTree: function(tree, formatter, config) {
config = config || {};
formatter = this.getFormatterByName(formatter);
var store = tree.store || config.store;
Ext.applyIf(config, {
title: tree.title
});
return formatter.format(store, config);
},
getFormatterByName: function(formatter) {
formatter = formatter ? formatter : "excel";
formatter = !Ext.isString(formatter) ? formatter : Ext.create("Ext.ux.exporter." + formatter + "Formatter." + Ext.String.capitalize(formatter) + "Formatter");
return formatter;
}
}
});