<template #default="scope">
<el-input
v-if="scope.row.pid != 0"
v-model="scope.row.amount"
type="text"
placeholder="请输入"
@input="limitInput(scope.row)"
></el-input>
</template>
function limitInput(row) {
let val = row.amount
// if (!val) {
// return
// }
let str = val.replace(/[^\d.]/g, '')
let relZero = /^0{2}$/ //开头连续多个0只保留一个
//开头连续多个0只保留一个 & 只允许输入一个小数点 & 小数点后输入最多输入两位小数
if (
relZero.test(str) ||
str.split('.').length > 2 ||
(str.split('.').length == 2 && str.split('.')[1].length > 2)
) {
str = str.slice(0, -1)
}
if (/^\.$/.test(str)) {
//开头输入小数点自动补充0
str = '0.'
}
if (/^[0]+[1-9]+$/.test(str)) {
//首位为0+非零数字,去掉首位0
str = str.substring(1)
}
row.amount = str
console.log(row.amount)
let total = 0
tableData.value.forEach((item) => {
if (item.amount) {
total = total + Number(item.amount)
}
})
if (total == 0) {
formData.value.amount = undefined
} else {
formData.value.amount = total.toFixed(2)
}
}