formatValue(value,row,item){ let val = (value && value.split("")) || []; let sNum = val.toString(); //先转换成字符串类型 if (sNum.indexOf('.') === 0) {//第一位就是 . sNum = '0' + sNum } sNum = sNum.replace(/[^\d.]/g,""); //清除“数字”和“.”以外的字符 sNum = sNum.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的 sNum = sNum.replace(".","$#$").replace(/\./g,"").replace("$#$","."); sNum = sNum.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数 //以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额 if(sNum.indexOf(".")< 0 && sNum !==""){ sNum = parseFloat(sNum); } this.$set(row,item,sNum) },调用部分 在element表格中
<el-table-column prop="hhaa14004" width="110px" sortable label="" :show-overflow-tooltip="true" align="left"> <template scope="scope"> <el-input v-model="scope.row.hhaa14004" @input="e=>formatValue(e,scope.row,'hhaa14004')" controls-position="right" style="width: 100%"></el-input> </template> <template slot="header" slot-scope="scope"> <span style="color:red;">发票单价</span> </template> </el-table-column>
标签:el,val,value,replace,input,sNum,输入,row From: https://www.cnblogs.com/ddqyc/p/17394650.html