class ReportTable extends DataTable { constructor(element = null) { super(element); this.tableHead = document.createElement("THEAD"); this.element.appendChild(this.tableHead); this.tableBody = document.createElement("TBODY"); this.element.appendChild(this.tableBody); this.tableFoot = document.createElement("TFOOT"); this.element.appendChild(this.tableFoot); } /** * Fills the table with the given data while respecting the given report information. * The data must be provided as a two-dimensional array. * The first row is expected to be the header row containing unlocalised column names. */ populateWithReport(report) { this.data = report.data; this.report = report; //Prepare and perform the aggregations. this.sums = [ ]; this.hasAggregation = false; this.aggregations = [ ]; this.aggregationLabelIndex = -1; this.aggregationLabelText = ""; if (this.data && this.data.length > 0) { for (var i = 0; i < this.data.length; i++) { var row = this.data[i]; for (var j = 0; j < row.length; j++) { var cell = row[j]; var fieldInformation = this.report.getFieldInformation(this.data[0][j]); if (i == 0) { //Prepare the aggregations. //Add the sum counter for each column. this.sums.push(0); if (fieldInformation.aggregation) { this.hasAggregation = true; this.aggregations.push(fieldInformation.aggregation); } else { this.aggregations.push(null); } if (fieldInformation.aggregationLabel) { this.aggregationLabelIndex = j; this.aggregationLabelText = fieldInformation.aggregationLabel; } } else { //Perform the aggregation. if (fieldInformation.aggregation == "sum") { this.sums[j] += cell; } } } } } this.rebuild(); } getDisplayTableData() { var displayTableData = [ ]; for (var i = 0; i < this.data.length; i++) { var rowData = this.data[i]; var displayTableRow = [ ]; for (var j = 0; j < rowData.length; j++) { var cell = rowData[j]; var fieldInformation; if (i == 0) { fieldInformation = this.report.getFieldInformation(cell); cell = fieldInformation.displayName; } else { fieldInformation = this.report.getFieldInformation(this.data[0][j]); cell = this.report.buildCSVCell(this.data[0][j], cell, fieldInformation, i); } if (!fieldInformation.hidden) { displayTableRow.push(cell); } } displayTableData.push(displayTableRow); } //Add the aggregation row if there are any aggregations. if (this.hasAggregation) { var aggregationRow = [ ]; for (var i = 0; i < this.data[0].length; i++) { var fieldInformation = this.report.getFieldInformation(this.data[0][i]); if (!fieldInformation.hidden) { if (i == this.aggregationLabelIndex) { aggregationRow.push(this.aggregationLabelText); } else { if (this.aggregations[i] == "sum") { var value = this.sums[i]; if (fieldInformation.type == "currency") { value = Math.round(value * 100) / 100; } aggregationRow.push(value); } else { aggregationRow.push(""); } } } } displayTableData.push(aggregationRow); } return displayTableData; } rebuild() { //Remove the old headers. while (this.tableHead.children.length > 0) { this.tableHead.removeChild(this.tableHead.firstChild); } //Add the new headers. if (this.data.length > 0) { var rowData = this.data[0]; var row = document.createElement("TR"); this.tableHead.appendChild(row); for (var i = 0; i < rowData.length; i++) { var fieldName = rowData[i]; var fieldInformation = this.report.getFieldInformation(fieldName); if (!fieldInformation.hidden) { var cell = document.createElement("TH"); this.report.buildTitleCell(fieldName, fieldInformation, cell); row.appendChild(cell); } } } //Remove the old data. while (this.tableBody.children.length > 0) { this.tableBody.removeChild(this.tableBody.firstChild); } //Add the new data. for (var i = 1; i < this.data.length; i++) { var rowData = this.data[i]; var row = document.createElement("TR"); this.tableBody.appendChild(row); for (var j = 0; j < rowData.length; j++) { var value = rowData[j]; var fieldName = this.data[0][j]; var fieldInformation = this.report.getFieldInformation(fieldName); if (!fieldInformation.hidden) { var cell = document.createElement("TD"); this.report.buildCell(fieldName, value, fieldInformation, cell, i); row.appendChild(cell); } } } //Remove the old aggregation row. while (this.tableFoot.children.length > 0) { this.tableFoot.removeChild(this.tableFoot.firstChild); } //Add the new aggregation row. if (this.hasAggregation) { var row = document.createElement("TR"); this.tableFoot.appendChild(row); for (var i = 0; i < this.data[0].length; i++) { var fieldName = this.data[0][i]; var fieldInformation = this.report.getFieldInformation(fieldName); if (fieldInformation.hidden) { continue; } var value = null; if (this.aggregations[i] == "sum") { value = this.sums[i]; //Round the value if it is a currency field. if (fieldInformation.type == "currency") { value = (Math.round(value * 100) / 100).toFixed(2); } } else if (i == this.aggregationLabelIndex) { value = this.aggregationLabelText; } var cell = document.createElement("TH"); this.report.buildAggregationCell(fieldName, value, fieldInformation, cell); row.appendChild(cell); } } } } UIKit.registerViewType(ReportTable);