添加自定义element金额输入框组件

master
Sigo 4 years ago
parent 4308095e50
commit 02a19df35f

@ -0,0 +1,106 @@
<!-- 金额输入框 -->
<template>
<el-input v-model.trim="form.moneyShow" :placeholder="placeholder" @blur="handleBlur" @focus="handleFocus" @input="handleInput" @paste.native.capture.prevent="handlePaste">
<i slot="prefix" class="el-input__icon el-icon-coin"></i>
</el-input>
</template>
<script>
export default {
data() {
return {
form: {
moneyShow: null,
money: null,
},
}
},
props: {
placeholder: String,
name: String,
},
methods: {
//
handleBlur() {
let form = Object.assign({}, this.form)
form.moneyShow = this.$thousands(this.form.money)
this.form = Object.assign({}, form)
},
//
handleFocus() {
let form = Object.assign({}, this.form)
form.moneyShow = form.money
this.form = Object.assign({}, form)
},
//
handleInput(value) {
let amount
//
let last = value.charAt(value.length - 1)
//
let cutLast = value.substring(0, value.length - 1)
let form = Object.assign({}, this.form)
let num = /^[0-9]*$/
if (last) {
if (num.test(last) || last == '.') {
if (last == '.') {
if (cutLast.includes('.')) {
amount = value.substring(0, value.length - 1)
} else {
amount = value
}
} else {
amount = value
}
} else {
amount = value.substring(0, value.length - 1)
}
}
form.moneyShow = amount
form.money = amount
this.form = Object.assign({}, form)
this.$emit('amount', this.name, amount)
},
//
handlePaste(res) {
let amount
let num = /^[0-9]*$/
let clip = res.clipboardData.getData('Text')
let form = Object.assign({}, this.form)
if (num.test(clip)) {
amount = form.money + clip
} else if (clip.includes('.')) {
if (clip.split('.').length > 2) {
amount = form.money
} else {
if (clip.split('.').length > 0) {
if (num.test(clip.split('.')[0]) && num.test(clip.split('.')[1])) {
if (form.money.includes('.')) {
amount = form.money
} else {
amount = form.money + clip
}
} else {
amount = form.money
}
} else {
amount = form.money
}
}
} else {
amount = form.money
}
form.moneyShow = amount
form.money = amount
this.form = Object.assign({}, form)
this.$emit('amount', this.name, amount)
},
//
reset() {
this.form = Object.assign({}, {})
},
},
}
</script>
<style></style>
Loading…
Cancel
Save