问题: 使用el-table和u-input组件做表格输入框时,需要对输入值做数字和小数校验,一开始想着使用type=number,可设置后移动端不兼容,故采input事件来校验,顺便记录一下动态列表格如何处理校验
<!-- html -->
<el-table :data="form.indicesTable" border style="width: 100%">
<el-table-column label="业务指标" prop="yName" align="center">
<template slot-scope="scope">
<text style="fontSize:24rpx;"><text class="red" v-if="scope.row.isRequire==='1'">*</text>{{scope.row.yName}}</text>
</template>
</el-table-column>
<el-table-column v-for="(item,index) in tableRowHead" align="center" :key="index" :label="item.lalel" :prop="item.value">
<template slot-scope="scope">
<u-input v-model.trim="scope.row[item.id]" maxlength="10" :clearable="true" placeholder="请输入" type="text" :border="false" @input="onInput(scope.$index,index)" />
</template>
</el-table-column>
</el-table>
// data
tableRowHead:[], // 列头数据
form:{
indicesTable:[] // 表格数据
}
// methods
// 数字和小数点和负号校验
onInput(rowIndex, colIndex) { // type=number无效,所以用input事件
let timer = setTimeout(() => { // 延迟执行,等待输入框的值更新
this.tableRowHead.forEach((item, index) => { //this.tableRowHead是接口获取的动态列头数据
if (colIndex == index) {
this.form.indicesTable[rowIndex][item.id] = this.form.indicesTable[rowIndex][item.id].replace(/[^\d.]/g, '');
}
})
clearTimeout(timer)
}, 0);
}