vue中el-input金額千分符自動(dòng)轉(zhuǎn)換的實(shí)現(xiàn)示例
1. 說明
在平時(shí)項(xiàng)目中,對于金額處理顯示一般需要按千分符顯示,通常實(shí)現(xiàn)會申明一個(gè)formater函數(shù)來進(jìn)行轉(zhuǎn)換,但是涉及的地方比較多試,使用起來比較繁瑣,封裝一個(gè)單獨(dú)的組件比較合理
2. 實(shí)現(xiàn)組件代碼
- ElMoneyInput.vue
<template> <div :style="{'background-color': disabled ? 'transparent' : '#fff'}"> <span class="money-input" v-if="!isInput" @click="focusHanle" :disabled="disabled" :value="viewValue">{{ viewValue }}</span> <el-input ref="moneyInput" v-else v-bind="$attrs" :value="value" @input="handlerChange" @blur="handlerBlur" autofocus onkeypress="if(event.keyCode == 13) return false;"/> </div> </template> <script> export default { name: 'ElMoneyInput', inheritAttrs: false, model: { prop: "value", event: "input" }, props: { value: { type: String, default: "" }, disabled:{ type:Boolean, default:false }, rules: { type: Object, default: () => {} } }, watch: { value(val, old) { if (val !== old) { this.init() } } }, mounted() { this.init() }, data() { return { isEdit: true, isInput: false, viewValue: '' }; }, methods: { init() { if (!this.isInput) { this.viewValue = this.formatMoney(this.value || 0) this.$emit('input', this.blurformat(this.value || 0)) } }, focusHanle() { if (this.disabled) return this.isInput = !this.isInput this.$emit('input', this.blurformat(this.value || 0)) this.$nextTick(() => { this.$refs.moneyInput.focus() }) }, formatMoney(cellValue, num = 2) { if (isNaN(cellValue)) { return "" } if (cellValue === 0) { return '0.00'; } return this.$Utils.formatMoney(cellValue, num); }, format(v) { return (`${v}`.match(/([\d\.]+)/) || "")[0]; }, blurformat(v) { return v ? Number.parseFloat(v).toFixed(2) : ""; }, handlerChange(v) { this.$emit('input', v) }, handlerBlur() { this.isInput = false; this.$emit('input', this.blurformat(this.value)) this.viewValue = this.formatMoney(this.value) }, // handleFocus() { // this.isInput = true; // this.$emit('input', this.blurformat(this.value)) // } } }; </script> <style lang="less"> .span-input{ display: inline-block; width: 100%; height:28px; } .money-input { position: relative; font-size: 14px; display: inline-block; height: 32px; line-height: 32px; background: transparent !important; cursor: text !important; background-color: #FFF; background-image: none; border-radius: 4px; border: 1px solid #DCDFE6; -webkit-box-sizing: border-box; box-sizing: border-box; color: #606266; display: inline-block; font-size: inherit; outline: 0; padding: 0 15px; -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1); transition: border-color .2s cubic-bezier(.645,.045,.355,1); width: 100%; .el-input__inner { background: transparent !important; cursor: text !important; background-color: #FFF; background-image: none; border-radius: 4px; border: 1px solid #DCDFE6; -webkit-box-sizing: border-box; box-sizing: border-box; color: #606266; display: inline-block; font-size: inherit; height: 40px; line-height: 40px; outline: 0; padding: 0 15px; -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1); transition: border-color .2s cubic-bezier(.645,.045,.355,1); width: 100%; } } </style>
3.實(shí)現(xiàn)效果
到此這篇關(guān)于vue中el-input金額千分符自動(dòng)轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)el-input金額千分符自動(dòng)轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法(推薦)
vue router給我們提供了兩種頁面間傳遞參數(shù)的方式,一種是動(dòng)態(tài)路由匹配,一種是編程式導(dǎo)航,接下來通過本文給大家介紹Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法,需要的朋友可以參考下2018-11-11vue之el-tree懶加載數(shù)據(jù)并且實(shí)現(xiàn)樹的過濾問題
這篇文章主要介紹了vue之el-tree懶加載數(shù)據(jù)并且實(shí)現(xiàn)樹的過濾問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue: WebStorm設(shè)置快速編譯運(yùn)行的方法
今天小編就為大家分享一篇vue: WebStorm設(shè)置快速編譯運(yùn)行的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10快速修改antd?vue單個(gè)組件的默認(rèn)樣式
這篇文章主要介紹了快速修改antd?vue單個(gè)組件的默認(rèn)樣式方式,具有很好的參考價(jià)值,希望對大家有所幫助。2022-08-08