Skip to content

Commit

Permalink
enhance pivot table element to be well formed for potential use with …
Browse files Browse the repository at this point in the history
…DataTables plugin
  • Loading branch information
astechdev committed Aug 12, 2015
1 parent 06e6236 commit b84f4e7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
53 changes: 34 additions & 19 deletions dist/pivot.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@
Default Renderer for hierarchical table layout
*/
pivotTableRenderer = function(pivotData, opts) {
var aggregator, c, colAttrs, colKey, colKeys, defaults, i, j, r, result, rowAttrs, rowKey, rowKeys, spanSize, td, th, totalAggregator, tr, txt, val, x;
var aggregator, c, colAttrs, colKey, colKeys, defaults, i, j, r, result, rowAttrs, rowKey, rowKeys, spanSize, thead, tbody, td, th, totalAggregator, tr, txt, val, x;
defaults = {
localeStrings: {
totals: "Totals"
Expand All @@ -748,6 +748,8 @@
colKeys = pivotData.getColKeys();
result = document.createElement("table");
result.className = "pvtTable";
thead = document.createElement("thead");
tbody = document.createElement("tbody");
spanSize = function(arr, i, j) {
var l, len, n, noDraw, ref, ref1, stop, x;
if (i !== 0) {
Expand Down Expand Up @@ -812,7 +814,7 @@
th.setAttribute("rowspan", colAttrs.length + (rowAttrs.length === 0 ? 0 : 1));
tr.appendChild(th);
}
result.appendChild(tr);
thead.appendChild(tr);
}
if (rowAttrs.length !== 0) {
tr = document.createElement("tr");
Expand All @@ -830,26 +832,38 @@
th.innerHTML = opts.localeStrings.totals;
}
tr.appendChild(th);
result.appendChild(tr);
thead.appendChild(tr);
}
for (i in rowKeys) {
if (!hasProp.call(rowKeys, i)) continue;
rowKey = rowKeys[i];
tr = document.createElement("tr");
for (j in rowKey) {
if (!hasProp.call(rowKey, j)) continue;
txt = rowKey[j];
x = spanSize(rowKeys, parseInt(i), parseInt(j));
if (x !== -1) {
th = document.createElement("th");
th.className = "pvtRowLabel";
th.innerHTML = txt;
th.setAttribute("rowspan", x);
if (parseInt(j) === rowAttrs.length - 1 && colAttrs.length !== 0) {
th.setAttribute("colspan", 2);
if (opts.datatablesEnabled === true ){
if (!hasProp.call(rowKey, j)) continue;
txt = rowKey[j];
th = document.createElement("th");
th.className = "pvtRowLabel";
th.innerHTML = txt;
tr.appendChild(th);
if (parseInt(j) === rowAttrs.length - 1 && colAttrs.length !== 0) {
tr.appendChild(document.createElement("th"));
}
} else {
if (!hasProp.call(rowKey, j)) continue;
txt = rowKey[j];
x = spanSize(rowKeys, parseInt(i), parseInt(j));
if (x !== -1) {
th = document.createElement("th");
th.className = "pvtRowLabel";
th.innerHTML = txt;
th.setAttribute("rowspan", x);
if (parseInt(j) === rowAttrs.length - 1 && colAttrs.length !== 0) {
th.setAttribute("colspan", 2);
}
tr.appendChild(th);
}
}
tr.appendChild(th);
}
}
for (j in colKeys) {
if (!hasProp.call(colKeys, j)) continue;
Expand All @@ -870,7 +884,7 @@
td.setAttribute("data-value", val);
td.setAttribute("data-for", "row" + i);
tr.appendChild(td);
result.appendChild(tr);
tbody.appendChild(tr);
}
tr = document.createElement("tr");
th = document.createElement("th");
Expand All @@ -897,6 +911,8 @@
td.innerHTML = totalAggregator.format(val);
td.setAttribute("data-value", val);
tr.appendChild(td);
result.appendChild(thead);
result.appendChild(tbody);
result.appendChild(tr);
result.setAttribute("data-numrows", rowKeys.length);
result.setAttribute("data-numcols", colKeys.length);
Expand All @@ -921,6 +937,7 @@
derivedAttributes: {},
renderer: pivotTableRenderer,
rendererOptions: null,
datatablesEnabled: false,
localeStrings: locales.en.localeStrings
};
opts = $.extend(defaults, opts);
Expand Down Expand Up @@ -1504,6 +1521,4 @@
};
});

}).call(this);

//# sourceMappingURL=pivot.js.map
}).call(this);
33 changes: 23 additions & 10 deletions pivot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ callWithJQuery ($) ->
#now actually build the output
result = document.createElement("table")
result.className = "pvtTable"
thead = document.createElement("thead");
tbody = document.createElement("tbody");

#helper function for setting row/col-span in pivotTableRenderer
spanSize = (arr, i, j) ->
Expand Down Expand Up @@ -458,15 +460,23 @@ callWithJQuery ($) ->
for own i, rowKey of rowKeys
tr = document.createElement("tr")
for own j, txt of rowKey
x = spanSize(rowKeys, parseInt(i), parseInt(j))
if x != -1
th = document.createElement("th")
th.className = "pvtRowLabel"
if opts.datatablesEnabled == true
th = document.createElement('th')
th.className = 'pvtRowLabel'
th.innerHTML = txt
th.setAttribute("rowspan", x)
if parseInt(j) == rowAttrs.length-1 and colAttrs.length !=0
th.setAttribute("colspan",2)
tr.appendChild th
if parseInt(j) == rowAttrs.length-1 and colAttrs.length !=0
tr.appendChild document.createElement('th')
else
x = spanSize(rowKeys, parseInt(i), parseInt(j))
if x != -1
th = document.createElement("th")
th.className = "pvtRowLabel"
th.innerHTML = txt
th.setAttribute("rowspan", x)
if parseInt(j) == rowAttrs.length-1 and colAttrs.length !=0
th.setAttribute("colspan",2)
tr.appendChild th
for own j, colKey of colKeys #this is the tight loop
aggregator = pivotData.getAggregator(rowKey, colKey)
val = aggregator.value()
Expand Down Expand Up @@ -504,11 +514,13 @@ callWithJQuery ($) ->
tr.appendChild td
totalAggregator = pivotData.getAggregator([], [])
val = totalAggregator.value()
td = document.createElement("td")
td.className = "pvtGrandTotal"
td = document.createElement('td')
td.className = 'pvtGrandTotal'
td.innerHTML = totalAggregator.format(val)
td.setAttribute("data-value", val)
tr.appendChild td
result.appendChild thead
result.appendChild tbody
result.appendChild tr

#squirrel this away for later
Expand All @@ -532,7 +544,8 @@ callWithJQuery ($) ->
sorters: ->
derivedAttributes: {},
renderer: pivotTableRenderer
rendererOptions: null
rendererOptions: null,
datatablesEnabled: false,
localeStrings: locales.en.localeStrings

opts = $.extend defaults, opts
Expand Down

0 comments on commit b84f4e7

Please sign in to comment.