利用element-ui编写一个表单金额输入框-创新互联
这篇文章给大家介绍利用element-ui编写一个表单金额输入框,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

组件代码如下:
},
rules: {
type: Object,
default: () => { },
},
},
data () {
return {
showFormatPrice: false, // 是否显示遮罩
}
},
computed: {
formaterPrice () {
if (
this.form.deceivedAmount !== '' &&
this.form.deceivedAmount !== null
) {
// 去掉前面的0
const integer = this.form.deceivedAmount.split('.')[0]
const decimal = this.form.deceivedAmount.split('.')[1]
? `.${this.form.deceivedAmount.split('.')[1]}`
: ''
return `${integer
.toString()
.replace(/(?=(?!^)(\d{3})+$)/g, ',')}${decimal}`
} else {
return ''
}
},
},
methods: {
// 聚焦金额输入框
focusInput () {
this.showFormatPrice = false
this.$refs.input.focus()
},
// 失焦金额输入框
blurInput () {
if (this.form.deceivedAmount !== '') {
// 去掉前面的0
const integer = Number(this.form.deceivedAmount.split('.')[0])
const decimal = this.form.deceivedAmount.split('.')[1]
? `.${this.form.deceivedAmount.split('.')[1]}`
: ''
this.form.deceivedAmount = isNaN(`${integer}${decimal}`)
? this.form.deceivedAmount
: `${integer}${decimal}`
if (typeof this.rules[this.prop][0].pattern !== 'object') {
throw `请确保 rules[${this.prop}][0].pattern 为正则表达式`
return
}
this.showFormatPrice = this.rules[this.prop][0].pattern.test(
this.form.deceivedAmount,
)
}
},
},
}
在表单中的使用方法其实和你直接写一个el-form-item 的效果是一样的,直接引入即可。
// 使用方法: el-form(:model="form" ref="form" label="180px" :label-suffix="':'" :rules="rules") priceInput(:form.sync = "form" :width = "150" label = "金额" prop = "deceivedAmount" :rules = "rules")
关于利用element-ui编写一个表单金额输入框就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前名称:利用element-ui编写一个表单金额输入框-创新互联
网页链接:http://www.jxjierui.cn/article/dicsih.html


咨询
建站咨询
