-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeinPivot.js
More file actions
346 lines (292 loc) · 7.92 KB
/
meinPivot.js
File metadata and controls
346 lines (292 loc) · 7.92 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* MeinPivot - generate Pivot tables with unlimited number of Pivoting fields.
* Javascript/jQuery version.
*
* This class was inspired by the great gam-pivot php class made by
* Gonzalo Ayuso (gonzalo123), that can be found in GitHub:
*
* https://github.com/gonzalo123/gam-pivot
*
* The main purpose of making a new class was to make possible the pivoting of
* columns to unlimited rows.
*
* Website: https://github.com/djjorjinho/meinPivot
* License: BSD 2-Clause License
*/
(function($){
/**
* Default options for the pivot table generation.
*
*/
var defaults = {
data: [], // resultset from an SQL query (array with objects)
columns: [],
rows: [],
measures: [],
destroyTable: false,
funcCache: {},
tableContainer:'',
concatString: ' | '
};
/**
* Constructor. Takes options and thats pretty much it.
*/
var meinPivot = function(options){
this.options = $.extend({},defaults,options);
};
meinPivot.prototype = {
/**
* Generates a data recordset based on a table with the
* appropriate class hierarchy:
* .pivot-table-metadata
* .pivot-table-column
* ...
* .pivot-table-row
* ...
* .pivot-table-measure
* ...
* .pivot-table-data
* ...
* ...
* The header -> data relation is based on the order
* where the header fields are found on the DOM.
* Ex: Column 'Year' is the second child on the metadata DOM
* so the data for the 'Year' must be the second child in the data DOM.
*
*/
extractDataFromTable : function(table){
var self = this;
var newData = [];
var fields = {};
table.find('.pivot-table-column,.pivot-table-row,.pivot-table-measure'
,'.pivot-table-metadata')
.each(function(i,dom){
var elm = $(dom);
fields[elm.text()] = elm.index();
if(elm.hasClass('pivot-table-column'))
self.options.columns.push(elm.text());
else if(elm.hasClass('pivot-table-row'))
self.options.rows.push(elm.text());
else if(elm.hasClass('pivot-table-measure'))
self.options.measures.push(elm.text());
});
table.find('.pivot-table-data')
.each(function(i,dom){
var elm = $(dom);
var record = {};
$.each(fields,function(field,idx){
record[field] = elm.find(':eq('+idx+')').text() || '';
});
newData.push(record);
});
self.options.data = newData;
if(self.options.destroyTable)
table.remove();
return self;
},
table: function(){
var self = this;
var pivoted = self.getPivot();
if(self.options.tableContainer=='')
return pivoted;
return self.simpleHtmlTable(pivoted);
},
simpleHtmlTable : function(data){
var self = this;
var rows = self.options.rows;
var columns = self.options.columns;
var rlen = rows.length;
var clen = columns.length;
var mlen = self.options.measures.length;
var concat = self.options.concatString;
var regex = new RegExp(concat);
out="";
out+="<table class='pivot-table-result'>";
out+= "<thead>";
// headers
var dcnt = 0;
$.each(data[0],function(item,i){
if(!regex.test(item))
return 0;
dcnt = item.split(concat).length-1;
return -1;
});
//var cnt = self.keys(data[0]).length-rlen;
var hcnt = 0;
for(var idx=0; idx<dcnt; idx++){
out += "<tr>";
$.each(rows,function(r,row){
out += "<td></td>";
});
$.each(data[0],function(item,i){
if(!regex.test(item))
return;
var split = item.split(concat)[hcnt];
out += "<td class='pivot-table-result-header'>"
+split+"</td>";
});
out += "</tr>";
hcnt++;
}
// bottom header
$.each(data[0],function(item,i){
if(!regex.test(item)){
out += "<td>"+item+"</td>";
return;
}
var split = item.split(concat)[hcnt];
out += "<td class='pivot-table-result-header'>"
+split+"</td>";
});
out+= "</thead>";
for (var row in data) {
var cnt = rlen;
out+= "<tr>";
for (var item2 in data[row]) {
var cls = cnt > 0
? 'pivot-table-result-row'
: 'pivot-table-result-value';
var val = data[row][item2]
val= (val==undefined) ? '': val;
out+= "<td class='"+cls+"'>"+val+"</td>";
cnt--;
}
out+= "</tr>";
}
out+= "</table>";
jQuery(self.options.tableContainer).html(out);
return self;
},
keys: function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
},
getPivot: function(){
var self = this;
var options = self.options;
var tmp ={};
var splits = {};
var ref,sref;
$.each(options.data,function(i,item){
$.each(options.measures,function(m,measure){
ref = tmp;
sref = splits;
// assigning row split keys
$.each(options.rows,function(r,row){
var rowKey = item[options.rows[r]];
ref[rowKey] = ref[rowKey] || {};
ref = ref[rowKey];
});
// assigning column split keys
$.each(options.columns,function(c,column){
var columnKey = item[column];
ref[columnKey] = ref[columnKey] || {};
ref = ref[columnKey];
// only column splits assoc. array
sref[columnKey] = sref[columnKey] || {};
sref = sref[columnKey];
});
// assigning values
ref[measure] = ref[measure] || 0;
var mvalue = parseFloat(item[measure]);
ref[measure] += isNaN(mvalue) ? 0 : mvalue;
sref[measure] = measure;
});
});
return self.buildPivot(tmp,splits);
},
/**
* Take the processed data and build a new array of objects,
* proper for a HTML table.
*
*/
buildPivot: function(tmp,splits){
var self = this;
var options = self.options;
var columns = options.columns;
var rows = options.rows;
var measures = options.measures;
var concat = options.concatString;
var ckey = columns.length+':'+rows.length+':'+measures.length;
var out = [];
var func;
if(options.funcCache[ckey])
return options.funcCache[ckey]
.exec(out,columns,rows,measures,tmp,splits);
var code = 'func={exec:function(out,columns,rows,measures,tmp,splits){';
// open rows
$.each(rows,function(r,row){
if(r==0)
code += "$.each(tmp,function(p"+r+",p"+r+"Values){";
else
code += "$.each(p"+(r-1)+"Values,function(p"+r+",p"+r+"Values){";
});
// variables
code += "var _out = {};";
$.each(rows,function(r,row){
code += "_out[rows["+r+"]] = p"+r+";";
});
var _aux = [];
// open columns
$.each(columns,function(c,column){
if(c==0){
code += "$.each(splits,function(s0){";
code += "var spl0 = splits[s0];";
code += "var colValues = p"+(rows.length-1)+"Values;";
}else{
var i = c-1;
code += "$.each(spl"+i+",function(s"+c+"){";
code += "var spl"+c+" = spl"+i+"[s"+c+"];";
}
_aux.push('s'+c);
});
var arraux = '['+_aux.join('][')+']';
// open measures
code += "$.each(measures,function(m,measure){";
code += "var value;";
code += "try{ value = colValues"+arraux+"[measure] || '';}";
code += "catch(err){value='';};";
code += "_out["+_aux.join("+'"+concat+"'+")+"+'"+concat+"'+measure] "
+"= value;";
// close measures
code += "});";
// close columns
$.each(columns,function(c,column){
code += "});";
});
code += "out.push(_out);";
// close rows
$.each(rows,function(r,row){
code += "});";
});
code += "return out;";
code += '}};';
eval(code);
// cache it
options.funcCache[ckey] = func;
return func.exec(out,columns,rows,measures,tmp,splits);
}
};
/**
* JQuery pivot table factory.
* Set the data yourself in the options.
*
*/
$.meinPivot = function(options){
return new meinPivot(options);
};
/**
* JQuery style pivot table factory.
* Select a table element and specify the options.
* The data, columns, rows and measures will be extracted from the html table.
*/
$.fn.meinPivot = function(options){
var mp = new meinPivot(options);
// one table element at a time boys...
mp.extractDataFromTable($(this.get(0)));
return mp;
};
}(jQuery));