From 4cb48e537fd0d6cdc85bfdaeec8af1a699a6a63b Mon Sep 17 00:00:00 2001 From: zhangzhen <525765282@qq.com> Date: Tue, 5 Aug 2025 10:20:59 +0800 Subject: [PATCH] 同步_Sum功能; --- src/components/Common/CommonTable/index.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/components/Common/CommonTable/index.less | 4 ++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/components/Common/CommonTable/index.js b/src/components/Common/CommonTable/index.js index a1d1083..ddb3309 100644 --- a/src/components/Common/CommonTable/index.js +++ b/src/components/Common/CommonTable/index.js @@ -292,6 +292,9 @@ class CommonTableRc extends React.Component { } document.addEventListener('mouseover', this.handleLastTdMouseOver); + + // 监听鼠标右击事件 + this.mydiv.addEventListener('contextmenu', this.handleContextMenu); } componentDidUpdate() { @@ -411,7 +414,7 @@ class CommonTableRc extends React.Component { this.forceUpdate(); }); document.removeEventListener('mouseover', this.handleLastTdMouseOver); - + this.mydiv.removeEventListener('contextmenu', this.handleContextMenu); if (this.tableId && window[`${this.tableId}FieldPopupModal`]) { delete window[`${this.tableId}FieldPopupModal`]; } @@ -825,6 +828,25 @@ class CommonTableRc extends React.Component { }; }; + handleContextMenu = (event) => { + // 判断右键位置是否有选中文本 + if (!window.getSelection().toString()) { + const { target } = event; + const { tagName } = target; + const oDiv = { + 'span': target.parentNode, + 'td': target.childNodes[0].childNodes[0], + 'div': target.getAttribute('data-name') ? target : target.childNodes[0], + }[tagName.toLowerCase()]; + + if (!oDiv) return; + if (!oDiv.getAttribute('data-control-name')?.includes('_Sum')) return; + // 阻止右键菜单 + event.preventDefault(); + this.setState({ totalDataNew: undefined, sumGroup: {} }); + } + + } onRowMouseEnter= (record) => { // recor if (this.props.enabled && this.props.tableBelone !== 'list' && commonUtils.isNotEmptyObject(this.props.tableProps) && @@ -856,6 +878,8 @@ class CommonTableRc extends React.Component { /** 行选择 */ onRowClick = (record, index, type, name, tabType) => { + if (this.handleCellClick(record)) return; + if (this.stopRowClick) { this.stopRowClick = false; return; @@ -4114,6 +4138,66 @@ class CommonTableRc extends React.Component { }); } + handleCellClick = (record) => { + const { enabled } = this.props; + if (enabled) return false; + + const { target } = event; + const { tagName } = target; + const oDiv = { + 'span': target.parentNode, + 'td': target.childNodes[0].childNodes[0], + 'div': target.getAttribute('data-name') ? target : target.childNodes[0], + }[tagName.toLowerCase()]; + + if (!oDiv || window.getSelection().toString()) return false; + + const [sName, sControlName] = [oDiv.getAttribute('data-name'), oDiv.getAttribute('data-control-name')]; + if (sControlName?.includes('_Sum')) { + const { sumGroup = {}, totalData = [{}], totalDataNew: totalDataNew0 = [{}] } = this.state; + const { dNetMoney, dNetPrice } = this.props.app.decimals; + sumGroup[sName] = sumGroup[sName] || []; + if (sumGroup[sName].includes(record[this.rowKey])) { + sumGroup[sName] = sumGroup[sName].filter(item => item !== record[this.rowKey]); + } else { + sumGroup[sName].push(record[this.rowKey]); + } + + let totalDataNew = [...totalDataNew0]; + if (sumGroup[sName].length) { + const sum = sumGroup[sName].reduce((pre, cur) => { + const curData = this.props.data.find(item => item[this.rowKey] === cur); + if (curData) { + let tempValue = 0; + if (sName.toLowerCase().endsWith('price')) { /* 价格 */ + tempValue = commonUtils.convertFixNum(commonUtils.convertFixNum(commonUtils.isNull(curData[sName], 0), dNetPrice) + commonUtils.convertFixNum(commonUtils.isNull(pre, 0), dNetPrice), dNetPrice); + } else { /* 金额 */ + tempValue = commonUtils.convertFixNum(commonUtils.convertFixNum(commonUtils.isNull(curData[sName], 0), dNetMoney) + commonUtils.convertFixNum(commonUtils.isNull(pre, 0), dNetMoney), dNetMoney); + } + return tempValue; + } + return pre; + }, 0); + + totalDataNew = [{ ...totalData[0], ...totalDataNew[0], [sName]: sum }]; + } else { + totalDataNew[0][sName] = totalData[0][sName]; + } + + this.setState({ sumGroup, totalDataNew }); + return true; + } + return false; + } + + handleGetSumStyle = (sName, index) => { + const { data = [] } = this.props; + const { sumGroup = {} } = this.state; + const sumList = sumGroup[sName] || []; + const record = data[index]; + return sumList.includes(record[this.rowKey]) ? " tableSumColor" : ""; + } + /* 点击图片预览 */ handlePreviewImage= (e, dataUrlArr) => { e.stopPropagation(); /* 阻止父级穿透 */ @@ -4343,7 +4427,8 @@ class CommonTableRc extends React.Component { } genFooter= () => { - const { tableColumn, totalData, totalData1 } = this.state; + const { tableColumn, totalData: totalData0, totalDataNew, sumGroup = {}, totalData1 } = this.state; + const totalData = Object.keys(sumGroup).some(key => sumGroup[key]?.length) ? totalDataNew : totalData0; const { dNetMoney, dNetPrice } = this.props.app.decimals; const sModelsType = commonUtils.isNotEmptyObject(this.props) && commonUtils.isNotEmptyObject(this.props.app) ? this.props.app.currentPane.sModelsType : ''; const sumset = commonFunc.showMessage(this.props.app.commonConst, 'sumSet') || '合计'; @@ -5448,6 +5533,8 @@ class CommonTableRc extends React.Component { style={{ position: 'relative', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', color: `${cellColor}`, backgroundColor: `${backgroundColor}`, textAlign:`${displayAlign}`, }} + data-name={showConfig.sName} + data-control-name={showConfig.sControlName} > {(commonUtils.isNotEmptyObject(sName) && sName.substring(0, 1) === 'b') || (imgBox !== '') ? '' : table > .ant-table-tbody > tr > td.tableSumColor{ + background-color: #BEE7E9 !important; + } } -- libgit2 0.22.2