Permalink
Browse files

Finalize crosstab functionality.

  • Loading branch information...
1 parent 0715c59 commit 4d1cde40189ff425877b8b1178c34be9e82b9c29 Robert Jackson committed Apr 13, 2012
Showing with 63 additions and 35 deletions.
  1. +7 −9 docs/source/pivot.html
  2. +1 −1 index.html
  3. +37 −6 jquery_pivot.js
  4. +7 −9 pivot.js
  5. +1 −1 pivot.min.js
  6. +3 −0 spec/pivotjs_spec.js
  7. +1 −2 src/display.js
  8. +6 −7 src/results.js
@@ -844,8 +844,6 @@
if (objectType(field) === 'string')
field = fields[field];
- results = undefined; resultsColumns = undefined;
-
displayFields[type][field.name] = field;
};
@@ -857,6 +855,7 @@
*/
function setDisplayFields(type, listing){
displayFields[type] = {};
+ results = undefined; resultsColumns = undefined;
var i = -1, m = listing.length;
while (++i < m) {
@@ -934,7 +933,7 @@
for (var key in displayFields.rowLabels) {
if (displayFields.rowLabels.hasOwnProperty(key)) {
- if (i === 0) resultsColumns.push({fieldName: key, width: 1});
+ if (i === 0) resultsColumns.push({fieldName: key, width: 1, type: 'row'});
resultKey += key + ':' + row[key] + '|';
}
@@ -955,11 +954,10 @@
};
function processColumnLabelResults(){
- for (var resultKey in results) {
- for (var key in displayFields.columnLabels) {
+ for (var key in displayFields.columnLabels) {
+ if (displayFields.columnLabels.hasOwnProperty(key)) {
var columnLabelColumns = {};
-
- if (displayFields.columnLabels.hasOwnProperty(key)) {
+ for (var resultKey in results) {
var values = pluckValues(results[resultKey].rows, fields[key]);
for (var value in values){
@@ -1032,7 +1030,7 @@
function populateSummaryColumnsResults(){
for (var key in displayFields.summaries){
if (displayFields.summaries.hasOwnProperty(key))
- resultsColumns.push({fieldName: key, width: 1})
+ resultsColumns.push({fieldName: key, width: 1, type: 'summary'})
}
return resultsColumns;
@@ -1045,7 +1043,7 @@
w = objectKeys(displayFields.summaries).length;
while (++i < m){
- resultsColumns.push({fieldName: keys[i], width: w})
+ resultsColumns.push({fieldName: keys[i], width: w, type: 'column'})
};
View
@@ -155,7 +155,7 @@
var date = new Date(row.invoice_date);
return date.getFullYear() + '_' + pivot.utils().padLeft((date.getMonth() + 1),2,'0')}
},
- {name: 'invoice_yyyy', type: 'string', filterable: true, pseudo: true,
+ {name: 'invoice_yyyy', type: 'string', filterable: true, pseudo: true, columnLabelable: true,
pseudoFunction: function(row){ return new Date(row.invoice_date).getFullYear() }},
{name: 'age_bucket', type: 'string', filterable: true, columnLabelable: true, pseudo: true, dataSource: 'last_payment_date', pseudoFunction: ageBucket},
View
@@ -195,28 +195,59 @@ var methods = {
update_results : function(){
var results = pivot.results().all(),
config = pivot.config(),
- columns = [].concat(config.rowLabels, config.columnLabels, config.summaries),
+ columns = pivot.results().columns(), //[].concat(config.rowLabels, config.columnLabels, config.summaries),
snip = '',
fieldName;
var result_table = $('#results'),
result_rows;
result_table.empty();
- snip += '<table class="table table-striped table-condensed"><thead><tr>';
+ snip += '<table class="table table-striped table-condensed"><thead>';
- $.each(columns, function(index, fieldName){
- snip += '<th>' + fieldName + '</th>';
+ // build columnLabel header row
+ if (config.columnLabels.length > 0) {
+ snip += '<tr>'
+ if (config.rowLabels.length > 0) {
+ snip += '<th colspan="' + config.rowLabels.length + '">&nbsp;</th>';
+ };
+ $.each(columns, function(index, column){
+ if (column.type === 'column')
+ snip += '<th colspan="' + column.width + '">' + column.fieldName + '</th>';
+ });
+ snip += '</tr>'
+ }
+
+ snip += '<tr>'
+ $.each(columns, function(index, column){
+ if (column.type !== 'column') {
+ snip += '<th>' + column.fieldName + '</th>';
+ } else {
+ $.each(config.summaries, function(index, fieldName){
+ snip += '<th>' + fieldName + '</th>';
+ });
+ }
});
+ snip += '</tr>'
+
snip += '</thead></tr><tbody id="result-rows"></tbody></table>';
result_table.append(snip);
result_rows = $('#result-rows');
$.each(results,function(index, row){
snip = '<tr>';
- $.each(columns, function(index, fieldName){
- snip += '<td>' + row[fieldName] + '</td>';
+ $.each(columns, function(index, column){
+ if (column.type !== 'column')
+ snip += '<td>' + row[column.fieldName] + '</td>';
+ else {
+ $.each(config.summaries, function(index, fieldName){
+ if (row[column.fieldName] !== undefined)
+ snip += '<td>' + row[column.fieldName][fieldName] + '</td>';
+ else
+ snip += '<td>&nbsp;</td>';
+ });
+ }
});
snip += '</tr>';
View
@@ -827,8 +827,6 @@ function pivotData(type) {
if (objectType(field) === 'string')
field = fields[field];
- results = undefined; resultsColumns = undefined;
-
displayFields[type][field.name] = field;
};
@@ -840,6 +838,7 @@ function pivotData(type) {
*/
function setDisplayFields(type, listing){
displayFields[type] = {};
+ results = undefined; resultsColumns = undefined;
var i = -1, m = listing.length;
while (++i < m) {
@@ -917,7 +916,7 @@ function pivotData(type) {
for (var key in displayFields.rowLabels) {
if (displayFields.rowLabels.hasOwnProperty(key)) {
- if (i === 0) resultsColumns.push({fieldName: key, width: 1});
+ if (i === 0) resultsColumns.push({fieldName: key, width: 1, type: 'row'});
resultKey += key + ':' + row[key] + '|';
}
@@ -938,11 +937,10 @@ function pivotData(type) {
};
function processColumnLabelResults(){
- for (var resultKey in results) {
- for (var key in displayFields.columnLabels) {
+ for (var key in displayFields.columnLabels) {
+ if (displayFields.columnLabels.hasOwnProperty(key)) {
var columnLabelColumns = {};
-
- if (displayFields.columnLabels.hasOwnProperty(key)) {
+ for (var resultKey in results) {
var values = pluckValues(results[resultKey].rows, fields[key]);
for (var value in values){
@@ -1015,7 +1013,7 @@ function pivotData(type) {
function populateSummaryColumnsResults(){
for (var key in displayFields.summaries){
if (displayFields.summaries.hasOwnProperty(key))
- resultsColumns.push({fieldName: key, width: 1})
+ resultsColumns.push({fieldName: key, width: 1, type: 'summary'})
}
return resultsColumns;
@@ -1028,7 +1026,7 @@ function pivotData(type) {
w = objectKeys(displayFields.summaries).length;
while (++i < m){
- resultsColumns.push({fieldName: keys[i], width: w})
+ resultsColumns.push({fieldName: keys[i], width: w, type: 'column'})
};
View
Oops, something went wrong.
@@ -286,8 +286,11 @@ describe('pivot', function () {
expect(pivot.results().columns().length).toEqual(13);
expect(pivot.results().columns()[1].fieldName).toEqual('2011_12');
expect(pivot.results().columns()[1].width).toEqual(1);
+ expect(pivot.results().all()[1][pivot.results().columns()[1].fieldName].billed_amount_sum).toEqual(7.45);
+
expect(pivot.results().columns()[2].fieldName).toEqual('2012_01');
expect(pivot.results().columns()[2].width).toEqual(1);
+ expect(pivot.results().all()[1][pivot.results().columns()[2].fieldName].billed_amount_sum).toEqual(100);
});
});
});
View
@@ -65,8 +65,6 @@
if (objectType(field) === 'string')
field = fields[field];
- results = undefined; resultsColumns = undefined;
-
displayFields[type][field.name] = field;
};
@@ -78,6 +76,7 @@
*/
function setDisplayFields(type, listing){
displayFields[type] = {};
+ results = undefined; resultsColumns = undefined;
var i = -1, m = listing.length;
while (++i < m) {
View
@@ -42,7 +42,7 @@
for (var key in displayFields.rowLabels) {
if (displayFields.rowLabels.hasOwnProperty(key)) {
- if (i === 0) resultsColumns.push({fieldName: key, width: 1});
+ if (i === 0) resultsColumns.push({fieldName: key, width: 1, type: 'row'});
resultKey += key + ':' + row[key] + '|';
}
@@ -63,11 +63,10 @@
};
function processColumnLabelResults(){
- for (var resultKey in results) {
- for (var key in displayFields.columnLabels) {
+ for (var key in displayFields.columnLabels) {
+ if (displayFields.columnLabels.hasOwnProperty(key)) {
var columnLabelColumns = {};
-
- if (displayFields.columnLabels.hasOwnProperty(key)) {
+ for (var resultKey in results) {
var values = pluckValues(results[resultKey].rows, fields[key]);
for (var value in values){
@@ -140,7 +139,7 @@
function populateSummaryColumnsResults(){
for (var key in displayFields.summaries){
if (displayFields.summaries.hasOwnProperty(key))
- resultsColumns.push({fieldName: key, width: 1})
+ resultsColumns.push({fieldName: key, width: 1, type: 'summary'})
}
return resultsColumns;
@@ -153,7 +152,7 @@
w = objectKeys(displayFields.summaries).length;
while (++i < m){
- resultsColumns.push({fieldName: keys[i], width: w})
+ resultsColumns.push({fieldName: keys[i], width: w, type: 'column'})
};

0 comments on commit 4d1cde4

Please sign in to comment.