UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

450 lines (449 loc) • 18.4 kB
/** * DevExtreme (esm/__internal/grids/data_grid/summary/m_summary.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import dataQuery from "../../../../common/data/query"; import storeHelper from "../../../../common/data/store_helper"; import { normalizeSortingInfo } from "../../../../common/data/utils"; import $ from "../../../../core/renderer"; import { extend } from "../../../../core/utils/extend"; import { each } from "../../../../core/utils/iterator"; import { isDefined, isPlainObject } from "../../../../core/utils/type"; import { ColumnsView } from "../../../grids/grid_core/views/columns_view"; import { CLASSES as VIEW_CLASSES } from "../../../grids/grid_core/views/const"; import gridCore from "../core"; import AggregateCalculator from "../m_aggregate_calculator"; import { DATAGRID_CELL_DISABLED, DATAGRID_FOOTER_ROW_CLASS, DATAGRID_GROUP_FOOTER_CLASS, DATAGRID_GROUP_FOOTER_ROW_TYPE, DATAGRID_GROUP_TEXT_CONTENT_CLASS, DATAGRID_NOWRAP_CLASS, DATAGRID_SUMMARY_ITEM_CLASS, DATAGRID_TEXT_CONTENT_CLASS, DATAGRID_TOTAL_FOOTER_CLASS, DATAGRID_TOTAL_FOOTER_ROW_TYPE } from "./const"; import { getSummaryOptions } from "./utils/get_summary_options"; export const renderSummaryCell = function(cell, options, setAria) { const $cell = $(cell); const { column: column } = options; const { summaryItems: summaryItems } = options; const $summaryItems = []; if (!column.command && summaryItems) { for (const summaryItem of summaryItems) { const text = gridCore.getSummaryText(summaryItem, options.summaryTexts); const $summaryItemElement = $("<div>").css("textAlign", summaryItem.alignment || column.alignment).addClass(DATAGRID_SUMMARY_ITEM_CLASS).addClass(DATAGRID_TEXT_CONTENT_CLASS).addClass(summaryItem.cssClass).toggleClass(DATAGRID_GROUP_TEXT_CONTENT_CLASS, "group" === options.rowType).text(text); setAria("label", `${column.caption??""} ${text??""}`, $summaryItemElement); $summaryItems.push($summaryItemElement) } $cell.append($summaryItems) } }; const getSummaryCellOptions = function(that, options) { const summaryTexts = that.option("summary.texts") || {}; return { totalItem: options.row, summaryItems: options.row.summaryCells[options.columnIndex], summaryTexts: summaryTexts } }; const forEachGroup = function(groups, groupCount, callback, path) { path = path || []; for (let i = 0; i < groups.length; i++) { path.push(groups[i].key); if (1 === groupCount) { callback(path, groups[i].items) } else { forEachGroup(groups[i].items, groupCount - 1, callback, path) } path.pop() } }; const applyAddedData = function(data, insertedData, groupLevel) { if (groupLevel) { return applyAddedData(data, insertedData.map(item => ({ items: [item] }), groupLevel - 1)) } return data.concat(insertedData) }; const applyRemovedData = function(data, removedData, groupLevel) { if (groupLevel) { return data.map(data => { const updatedData = {}; const updatedItems = applyRemovedData(data.items || [], removedData, groupLevel - 1); Object.defineProperty(updatedData, "aggregates", { get: () => data.aggregates, set: value => { data.aggregates = value } }); return extend(updatedData, data, { items: updatedItems }) }) } return data.filter(data => removedData.indexOf(data) < 0) }; const sortGroupsBySummaryCore = function(items, groups, sortByGroups) { if (!items || !groups.length) { return items } const group = groups[0]; const sorts = sortByGroups[0]; let query; if (group && sorts && sorts.length) { query = dataQuery(items); each(sorts, function(index) { if (0 === index) { query = query.sortBy(this.selector, this.desc) } else { query = query.thenBy(this.selector, this.desc) } }); query.enumerate().done(sortedItems => { items = sortedItems }) } groups = groups.slice(1); sortByGroups = sortByGroups.slice(1); if (groups.length && sortByGroups.length) { each(items, function() { this.items = sortGroupsBySummaryCore(this.items, groups, sortByGroups) }) } return items }; const sortGroupsBySummary = function(data, group, summary) { const sortByGroups = summary && summary.sortByGroups && summary.sortByGroups(); if (sortByGroups && sortByGroups.length) { return sortGroupsBySummaryCore(data, group, sortByGroups) } return data }; export class FooterView extends ColumnsView { _getRows() { return this._dataController.footerItems() } _getCellOptions(options) { return extend(super._getCellOptions(options), getSummaryCellOptions(this, options)) } _renderCellContent($cell, options) { renderSummaryCell($cell, options, this.setAria.bind(this)); super._renderCellContent.apply(this, arguments) } _renderCore(change) { let needUpdateScrollLeft = false; const totalItem = this._dataController.footerItems()[0]; if (!change || !change.columnIndices) { this.element().empty().addClass(DATAGRID_TOTAL_FOOTER_CLASS).toggleClass(DATAGRID_NOWRAP_CLASS, !this.option("wordWrapEnabled")); needUpdateScrollLeft = true } if (totalItem && totalItem.summaryCells && totalItem.summaryCells.length) { this._updateContent(this._renderTable({ change: change }), change); needUpdateScrollLeft && this.updateScrollLeftPosition() } return super._renderCore(change) } _updateContent($newTable, change) { if (change && "update" === change.changeType && change.columnIndices) { return this.waitAsyncTemplates().done(() => { const $row = this.getTableElement().find(`.${VIEW_CLASSES.row}`); const $newRow = $newTable.find(`.${VIEW_CLASSES.row}`); this._updateCells($row, $newRow, change.columnIndices[0]) }) } return super._updateContent.apply(this, arguments) } _rowClick(e) { const item = this._dataController.footerItems()[e.rowIndex] || {}; this.executeAction("onRowClick", extend({}, e, item)) } _columnOptionChanged(e) { const { optionNames: optionNames } = e; if (e.changeTypes.grouping) { return } if (optionNames.width || optionNames.visibleWidth) { super._columnOptionChanged(e) } } _handleDataChanged(e) { const { changeType: changeType } = e; if ("update" === e.changeType && e.repaintChangesOnly) { if (!e.totalColumnIndices) { this.render() } else if (e.totalColumnIndices.length) { this.render(null, { changeType: "update", columnIndices: [e.totalColumnIndices] }) } } else if ("refresh" === changeType || "append" === changeType || "prepend" === changeType) { this.render() } } _createRow(row) { const $row = super._createRow.apply(this, arguments); if (row.rowType === DATAGRID_TOTAL_FOOTER_ROW_TYPE) { $row.addClass(DATAGRID_FOOTER_ROW_CLASS); $row.addClass(DATAGRID_CELL_DISABLED); $row.attr("tabindex", 0) } return $row } getHeight() { return this.getElementHeight() } isVisible() { return !!this._dataController.footerItems().length } } export const summaryDataSourceAdapterExtender = Base => class extends Base { init(dataSource) { super.init(dataSource); this.columnsController = this.getController("columns"); this.editingController = this.getController("editing"); this._totalAggregates = [] } getSummary(remoteOperations) { const summary = this.option("summary"); if (!summary) { return } const result = getSummaryOptions({ summary: summary, sortByGroupSummaryInfo: this.option("sortByGroupSummaryInfo"), remoteOperations: remoteOperations ?? this.remoteOperations(), getUpdatedItemData: data => this.editingController.getUpdatedData(data), columnOption: id => this.columnsController.columnOption(id), groupColumns: this.columnsController.getGroupColumns(), component: this.component }); return result } totalAggregates() { return this._totalAggregates } isLastLevelGroupItemsPagingLocal() { const summary = this.getSummary(); const sortByGroupsInfo = null === summary || void 0 === summary ? void 0 : summary.sortByGroups(); return null === sortByGroupsInfo || void 0 === sortByGroupsInfo ? void 0 : sortByGroupsInfo.length } sortLastLevelGroupItems(items, groups, paths) { const groupedItems = storeHelper.multiLevelGroup(dataQuery(items), groups).toArray(); let result = []; paths.forEach(path => { forEachGroup(groupedItems, groups.length, (itemsPath, items) => { if (path.toString() === itemsPath.toString()) { result = result.concat(items) } }) }); return result } _customizeRemoteOperations(options) { const summary = this.getSummary(); if (summary) { if (options.remoteOperations.summary) { if (!options.isCustomLoading || options.storeLoadOptions.isLoadingAll) { if (options.storeLoadOptions.group) { if (options.remoteOperations.grouping) { options.storeLoadOptions.groupSummary = summary.groupAggregates } else if (summary.groupAggregates.length) { options.remoteOperations.paging = false } } options.storeLoadOptions.totalSummary = summary.totalAggregates } } else if (summary.totalAggregates.length || summary.groupAggregates.length && options.storeLoadOptions.group) { options.remoteOperations.paging = false } } super._customizeRemoteOperations.apply(this, arguments); const cachedExtra = options.cachedData.extra; if (null !== cachedExtra && void 0 !== cachedExtra && cachedExtra.summary && !options.isCustomLoading) { options.storeLoadOptions.totalSummary = void 0 } } calculateTotalAggregates(data, totalAggregates, groupAggregates, groupLevel) { let dataWithEditingChanges = data; if (this.option("summary.recalculateWhileEditing")) { const insertedData = this.editingController.getInsertedData(); if (insertedData.length) { dataWithEditingChanges = applyAddedData(dataWithEditingChanges, insertedData, groupLevel) } const removedData = this.editingController.getRemovedData(); if (removedData.length) { dataWithEditingChanges = applyRemovedData(dataWithEditingChanges, removedData, groupLevel) } } const calculator = new AggregateCalculator({ data: dataWithEditingChanges, totalAggregates: totalAggregates, groupAggregates: groupAggregates, groupLevel: groupLevel }); calculator.calculate(); return calculator.totalAggregates() } customizeLoadResultHandlerCore(options) { if (options.isCustomLoading && !options.storeLoadOptions.isLoadingAll) { super.customizeLoadResultHandlerCore(options); return } const groups = normalizeSortingInfo(options.storeLoadOptions.group ?? options.loadOptions.group ?? []); const remoteOperations = options.remoteOperations ?? {}; const summary = this.getSummary(remoteOperations); if (remoteOperations.summary) { if (!remoteOperations.paging && groups.length && summary) { if (!remoteOperations.grouping) { this.calculateTotalAggregates(options.data, [], summary.groupAggregates, groups.length) } options.data = sortGroupsBySummary(options.data, groups, summary) } } else if (!remoteOperations.paging && summary) { var _options$cachedData; const operationTypes = options.operationTypes ?? {}; const hasOperations = Object.keys(operationTypes).some(type => operationTypes[type]); if (!hasOperations || !(null !== (_options$cachedData = options.cachedData) && void 0 !== _options$cachedData && null !== (_options$cachedData = _options$cachedData.extra) && void 0 !== _options$cachedData && _options$cachedData.summary) || groups.length && summary.groupAggregates.length) { const totalAggregates = this.calculateTotalAggregates(options.data, summary.totalAggregates, summary.groupAggregates, groups.length); options.extra = isPlainObject(options.extra) ? options.extra : {}; options.extra.summary = totalAggregates; if (options.cachedData) { options.cachedData.extra = options.extra } } options.data = sortGroupsBySummary(options.data, groups, summary) } if (!options.isCustomLoading) { var _options$extra; this._totalAggregates = (null === (_options$extra = options.extra) || void 0 === _options$extra ? void 0 : _options$extra.summary) ?? this._totalAggregates } super.customizeLoadResultHandlerCore(options) } }; export const summaryEditingControllerExtender = Base => class extends Base { _refreshSummary() { if (this.option("summary.recalculateWhileEditing") && !this.isSaving()) { this._dataController.refresh({ load: true, changesOnly: true }) } } _addChange(params) { const result = super._addChange.apply(this, arguments); if (params.type) { this._refreshSummary() } return result } _removeChange() { const result = super._removeChange.apply(this, arguments); this._refreshSummary(); return result } cancelEditData() { const result = super.cancelEditData.apply(this, arguments); this._refreshSummary(); return result } }; export const summaryRowsViewExtender = Base => class extends Base { _createRow(row) { const $row = super._createRow.apply(this, arguments); row && $row.addClass(row.rowType === DATAGRID_GROUP_FOOTER_ROW_TYPE ? DATAGRID_GROUP_FOOTER_CLASS : ""); return $row } _renderCells($row, options) { super._renderCells.apply(this, arguments); if ("group" === options.row.rowType && options.row.summaryCells && options.row.summaryCells.length) { this._renderGroupSummaryCells($row, options) } } _hasAlignByColumnSummaryItems(columnIndex, options) { const column = options.columns[columnIndex]; const isGrouped = isDefined(column.groupIndex); const isVirtual = "virtual" === column.command; const hasSummaryCells = !!options.row.summaryCells[columnIndex].length; return !isGrouped && (isVirtual || hasSummaryCells) } _getAlignByColumnCellCount(groupCellColSpan, options) { let alignByColumnCellCount = 0; for (let i = 1; i < groupCellColSpan; i++) { const columnIndex = options.row.summaryCells.length - i; const hasAlignBySummary = this._hasAlignByColumnSummaryItems(columnIndex, options); if (hasAlignBySummary) { alignByColumnCellCount = i } } return alignByColumnCellCount } _renderGroupSummaryCells($row, options) { const $groupCell = $row.children().last(); const groupCellColSpan = Number($groupCell.attr("colSpan")) || 1; const alignByColumnCellCount = this._getAlignByColumnCellCount(groupCellColSpan, options); this._renderGroupSummaryCellsCore($groupCell, options, groupCellColSpan, alignByColumnCellCount) } _renderGroupSummaryCellsCore($groupCell, options, groupCellColSpan, alignByColumnCellCount) { if (alignByColumnCellCount > 0) { $groupCell.attr("colSpan", groupCellColSpan - alignByColumnCellCount); for (let i = 0; i < alignByColumnCellCount; i++) { const columnIndex = options.columns.length - alignByColumnCellCount + i; this._renderCell($groupCell.parent(), extend({ column: options.columns[columnIndex], columnIndex: this._getSummaryCellIndex(columnIndex, options.columns) }, options)) } } } _getSummaryCellIndex(columnIndex, columns) { return columnIndex } _getCellTemplate(options) { if (!options.column.command && !isDefined(options.column.groupIndex) && options.summaryItems && options.summaryItems.length) { return (cell, options) => renderSummaryCell(cell, options, this.setAria.bind(this)) } return super._getCellTemplate(options) } _getCellOptions(options) { const that = this; const parameters = super._getCellOptions(options); if (options.row.summaryCells) { return extend(parameters, getSummaryCellOptions(that, options)) } return parameters } };