element el-input directive數(shù)字進(jìn)行控制
使用自定義指令格式化el-input
背景
使用element開(kāi)發(fā)的過(guò)程中遇到循環(huán)的數(shù)據(jù)只能輸入數(shù)字,并且有不要小數(shù)點(diǎn),有需要小數(shù)點(diǎn)的
使用vue directive 進(jìn)行控制
開(kāi)發(fā)
頁(yè)面使用方式 v-numberInt:0="item.first_fee" 0為保留幾位小數(shù)
<tr v-for="(item,index) in form.valuation_rules" :key="index"> <td class="center" > <el-input v-if="form.valuation_type==1" v-numberInt:0="item.first_fee" v-model.trim="item.first_amount"></el-input> <el-input v-else v-model.trim="item.first_amount" v-numberInt:2="item.first_fee"></el-input> </td> <td class="center"> <el-input v-model.trim="item.first_fee" v-numberInt:2="item.first_fee"></el-input> </td> <td class="center"> {{item.additional_amount}} </td> <td class="center"> <el-input v-model.trim="item.additional_fee" v-numberInt:2="item.additional_fee"></el-input> </td> </tr>
因?yàn)橛玫氖莈lement 的el-input ,組件input外層包著一層div所以要使用const element = el.getElementsByTagName('input')[0]獲取 input對(duì)其監(jiān)聽(tīng)失焦 當(dāng)輸入的不是數(shù)字時(shí),失焦后會(huì)變成0,沒(méi)有使用directive update方法,比較簡(jiǎn)單directives.js
directives.js
Vue.directive('numberInt', { bind: function(el, binding, vnode) { const element = el.getElementsByTagName('input')[0] const len = binding.arg // 初始化設(shè)置 element.value = Number(element.value ).toFixed(len) // 失焦時(shí)候格式化 element.addEventListener('blur', function() { if (isNaN(element.value)) { vnode.data.model.callback(0) } else { vnode.data.model.callback(Number(element.value).toFixed(len)) } }) }})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決iView中時(shí)間控件選擇的時(shí)間總是少一天的問(wèn)題
下面小編就為大家分享一篇解決iView中時(shí)間控件選擇的時(shí)間總是少一天的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03安裝@vue/cli 報(bào)錯(cuò)npm WARN deprecated request
這篇文章主要介紹了安裝@vue/cli 報(bào)錯(cuò)npm WARN deprecated request@2.88.2問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03解決Vite無(wú)法分析出動(dòng)態(tài)import的類型,控制臺(tái)出警告的問(wèn)題
這篇文章主要介紹了解決Vite無(wú)法分析出動(dòng)態(tài)import的類型,控制臺(tái)出警告的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Vue3項(xiàng)目中配置TypeScript和JavaScript的兼容
在Vue3開(kāi)發(fā)中,常見(jiàn)的使用JavaScript(JS)編寫代碼,但也會(huì)有調(diào)整編寫語(yǔ)言使用TypeScript(TS)的需求,因此,在Vue3項(xiàng)目設(shè)置中兼容TS和JS是刻不容緩的重要任務(wù),2023-08-08在Vue 中實(shí)現(xiàn)循環(huán)渲染多個(gè)相同echarts圖表
這篇文章主要介紹了在Vue 中實(shí)現(xiàn)循環(huán)渲染多個(gè)相同echarts圖表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07