【优化】优化STable组件内使用needTotal计算总和小数点无法计算问题

This commit is contained in:
俞宝山
2024-05-13 01:39:21 +08:00
parent 5a2d36ed52
commit aebc02a91b

View File

@@ -520,12 +520,33 @@
return {
...item,
total: selectedRows.reduce((sum, val) => {
const total = sum + parseInt(get(val, item.dataIndex))
return isNaN(total) ? 0 : total
return addNumbers(sum, get(val, item.dataIndex))
}, 0)
}
})
}
// 如果开启了needTotal计算总和支持小数点
const addNumbers = (num1, num2) => {
// 将参数转换为数字
let num1Value = Number(num1)
let num2Value = Number(num2)
// 检查转换后的值是否为有效的数字如果不是搞成0别影响其他数据的正确性
if (isNaN(num1Value)) {
num1Value = 0
}
if (isNaN(num2Value)) {
num2Value = 0
}
// 计算小数点后的总位数
const num1DecimalPlaces = ('' + num1Value).split('.')[1] ? ('' + num1Value).split('.')[1].length : 0
const num2DecimalPlaces = ('' + num2Value).split('.')[1] ? ('' + num2Value).split('.')[1].length : 0
const decimalPlaces = Math.max(num1DecimalPlaces, num2DecimalPlaces)
// 将数字乘以10的幂使其变为整数进行相加然后除以相同的10的幂还原为原始的小数形式
return (
(Math.round(num1Value * Math.pow(10, decimalPlaces)) + Math.round(num2Value * Math.pow(10, decimalPlaces))) /
Math.pow(10, decimalPlaces)
)
}
// 清空 table 已选中项
const clearSelected = () => {
if (props.rowSelection) {