1 var _typeof = typeof Symbol ===
"function" && typeof Symbol.iterator ===
"symbol" ?
function (obj) {
return typeof obj; } :
function (obj) {
return obj && typeof Symbol ===
"function" && obj.constructor === Symbol && obj !== Symbol.prototype ?
"symbol" : typeof obj; };
5 var ColumnCalcs =
function ColumnCalcs(table) {
9 this.genColumn =
false;
10 this.topElement = this.createElement();
11 this.botElement = this.createElement();
14 this.topInitialized =
false;
15 this.botInitialized =
false;
20 ColumnCalcs.prototype.createElement =
function () {
21 var el = document.createElement(
"div");
22 el.classList.add(
"tabulator-calcs-holder");
26 ColumnCalcs.prototype.initialize =
function () {
27 this.genColumn =
new Column({ field:
"value" },
this);
31 ColumnCalcs.prototype.registerColumnField =
function () {};
34 ColumnCalcs.prototype.initializeColumn =
function (column) {
35 var def = column.definition;
38 topCalcParams: def.topCalcParams || {},
39 botCalcParams: def.bottomCalcParams || {}
44 switch (_typeof(def.topCalc)) {
46 if (this.calculations[def.topCalc]) {
47 config.topCalc = this.calculations[def.topCalc];
49 console.warn(
"Column Calc Error - No such calculation found, ignoring: ", def.topCalc);
54 config.topCalc = def.topCalc;
60 column.modules.columnCalcs = config;
61 this.topCalcs.push(column);
63 if (this.table.options.columnCalcs !=
"group") {
64 this.initializeTopRow();
70 switch (_typeof(def.bottomCalc)) {
72 if (this.calculations[def.bottomCalc]) {
73 config.botCalc = this.calculations[def.bottomCalc];
75 console.warn(
"Column Calc Error - No such calculation found, ignoring: ", def.bottomCalc);
80 config.botCalc = def.bottomCalc;
86 column.modules.columnCalcs = config;
87 this.botCalcs.push(column);
89 if (this.table.options.columnCalcs !=
"group") {
90 this.initializeBottomRow();
96 ColumnCalcs.prototype.removeCalcs =
function () {
99 if (this.topInitialized) {
100 this.topInitialized =
false;
101 this.topElement.parentNode.removeChild(this.topElement);
105 if (this.botInitialized) {
106 this.botInitialized =
false;
107 this.table.footerManager.remove(this.botElement);
112 this.table.rowManager.adjustTableSize();
116 ColumnCalcs.prototype.initializeTopRow =
function () {
117 if (!this.topInitialized) {
119 this.table.columnManager.getElement().insertBefore(this.topElement, this.table.columnManager.headersElement.nextSibling);
120 this.topInitialized =
true;
124 ColumnCalcs.prototype.initializeBottomRow =
function () {
125 if (!this.botInitialized) {
126 this.table.footerManager.prepend(this.botElement);
127 this.botInitialized =
true;
131 ColumnCalcs.prototype.scrollHorizontal =
function (left) {
133 scrollWidth = this.table.columnManager.getElement().scrollWidth - this.table.element.clientWidth;
135 if (this.botInitialized) {
136 this.botRow.getElement().style.marginLeft = -left +
"px";
140 ColumnCalcs.prototype.recalc =
function (rows) {
143 if (this.topInitialized || this.botInitialized) {
144 data = this.rowsToData(rows);
146 if (this.topInitialized) {
148 this.topRow.deleteCells();
151 row = this.generateRow(
"top", this.rowsToData(rows));
153 while (this.topElement.firstChild) {
154 this.topElement.removeChild(this.topElement.firstChild);
155 }this.topElement.appendChild(row.getElement());
156 row.initialize(
true);
159 if (this.botInitialized) {
161 this.botRow.deleteCells();
164 row = this.generateRow(
"bottom", this.rowsToData(rows));
166 while (this.botElement.firstChild) {
167 this.botElement.removeChild(this.botElement.firstChild);
168 }this.botElement.appendChild(row.getElement());
169 row.initialize(
true);
172 this.table.rowManager.adjustTableSize();
175 if (this.table.modExists(
"frozenColumns")) {
176 this.table.modules.frozenColumns.layout();
181 ColumnCalcs.prototype.recalcRowGroup =
function (row) {
182 this.recalcGroup(this.table.modules.groupRows.getRowGroup(row));
185 ColumnCalcs.prototype.recalcGroup =
function (group) {
190 if (group.calcs.bottom) {
191 data = this.rowsToData(group.rows);
192 rowData = this.generateRowData(
"bottom", data);
194 group.calcs.bottom.updateData(rowData);
195 group.calcs.bottom.reinitialize();
198 if (group.calcs.top) {
199 data = this.rowsToData(group.rows);
200 rowData = this.generateRowData(
"top", data);
202 group.calcs.top.updateData(rowData);
203 group.calcs.top.reinitialize();
210 ColumnCalcs.prototype.generateTopRow =
function (rows) {
211 return this.generateRow(
"top", this.rowsToData(rows));
214 ColumnCalcs.prototype.generateBottomRow =
function (rows) {
215 return this.generateRow(
"bottom", this.rowsToData(rows));
218 ColumnCalcs.prototype.rowsToData =
function (rows) {
221 rows.forEach(
function (row) {
222 data.push(row.getData());
229 ColumnCalcs.prototype.generateRow =
function (pos, data) {
231 rowData = this.generateRowData(pos, data),
234 if (
self.table.modExists(
"mutator")) {
235 self.table.modules.mutator.disable();
238 row =
new Row(rowData,
this,
"calc");
240 if (
self.table.modExists(
"mutator")) {
241 self.table.modules.mutator.enable();
244 row.getElement().classList.add(
"tabulator-calcs",
"tabulator-calcs-" + pos);
246 row.generateCells =
function () {
250 self.table.columnManager.columnsByIndex.forEach(
function (column) {
253 self.genColumn.setField(column.getField());
254 self.genColumn.hozAlign = column.hozAlign;
256 if (column.definition[pos +
"CalcFormatter"] &&
self.table.modExists(
"format")) {
258 self.genColumn.modules.format = {
259 formatter:
self.table.modules.format.getFormatter(column.definition[pos +
"CalcFormatter"]),
260 params: column.definition[pos +
"CalcFormatterParams"]
263 self.genColumn.modules.format = {
264 formatter:
self.table.modules.format.getFormatter(
"plaintext"),
270 self.genColumn.definition.cssClass = column.definition.cssClass;
273 var cell =
new Cell(
self.genColumn, row);
274 cell.column = column;
277 column.cells.push(cell);
280 if (!column.visible) {
292 ColumnCalcs.prototype.generateRowData =
function (pos, data) {
294 calcs = pos ==
"top" ? this.topCalcs : this.botCalcs,
295 type = pos ==
"top" ?
"topCalc" :
"botCalc",
299 calcs.forEach(
function (column) {
302 if (column.modules.columnCalcs && column.modules.columnCalcs[type]) {
303 data.forEach(function (item) {
304 values.push(column.getFieldValue(item));
307 paramKey = type +
"Params";
308 params = typeof column.modules.columnCalcs[paramKey] ===
"function" ? column.modules.columnCalcs[paramKey](values, data) : column.modules.columnCalcs[paramKey];
310 column.setFieldValue(rowData, column.modules.columnCalcs[type](values, data, params));
317 ColumnCalcs.prototype.hasTopCalcs =
function () {
318 return !!this.topCalcs.length;
321 ColumnCalcs.prototype.hasBottomCalcs =
function () {
322 return !!this.botCalcs.length;
326 ColumnCalcs.prototype.redraw =
function () {
328 this.topRow.normalizeHeight(
true);
331 this.botRow.normalizeHeight(
true);
336 ColumnCalcs.prototype.getResults =
function () {
341 if (this.table.options.groupBy &&
this.table.modExists(
"groupRows")) {
342 groups = this.table.modules.groupRows.getGroups(
true);
344 groups.forEach(
function (group) {
345 results[group.getKey()] =
self.getGroupResults(group);
349 top: this.topRow ? this.topRow.getData() : {},
350 bottom: this.botRow ? this.botRow.getData() : {}
358 ColumnCalcs.prototype.getGroupResults =
function (group) {
360 groupObj = group._getSelf(),
361 subGroups = group.getSubGroups(),
362 subGroupResults = {},
365 subGroups.forEach(
function (subgroup) {
366 subGroupResults[subgroup.getKey()] =
self.getGroupResults(subgroup);
370 top: groupObj.calcs.top ? groupObj.calcs.top.getData() : {},
371 bottom: groupObj.calcs.bottom ? groupObj.calcs.bottom.getData() : {},
372 groups: subGroupResults
379 ColumnCalcs.prototype.calculations = {
380 "avg":
function avg(values, data, calcParams) {
382 precision = typeof calcParams.precision !==
"undefined" ? calcParams.precision : 2;
385 output = values.reduce(
function (sum, value) {
386 value = Number(value);
390 output = output / values.length;
392 output = precision !==
false ? output.toFixed(precision) : output;
395 return parseFloat(output).toString();
397 "max":
function max(values, data, calcParams) {
399 precision = typeof calcParams.precision !==
"undefined" ? calcParams.precision :
false;
401 values.forEach(
function (value) {
403 value = Number(value);
405 if (value > output || output === null) {
410 return output !== null ? precision !==
false ? output.toFixed(precision) : output :
"";
412 "min":
function min(values, data, calcParams) {
414 precision = typeof calcParams.precision !==
"undefined" ? calcParams.precision :
false;
416 values.forEach(
function (value) {
418 value = Number(value);
420 if (value < output || output === null) {
425 return output !== null ? precision !==
false ? output.toFixed(precision) : output :
"";
427 "sum":
function sum(values, data, calcParams) {
429 precision = typeof calcParams.precision !==
"undefined" ? calcParams.precision :
false;
432 values.forEach(
function (value) {
433 value = Number(value);
435 output += !isNaN(value) ? Number(value) : 0;
439 return precision !==
false ? output.toFixed(precision) : output;
441 "concat":
function concat(values, data, calcParams) {
445 output = values.reduce(
function (sum, value) {
446 return String(sum) + String(value);
452 "count":
function count(values, data, calcParams) {
456 values.forEach(
function (value) {
467 Tabulator.prototype.registerModule(
"columnCalcs", ColumnCalcs);