element el-input directive數(shù)字進(jìn)行控制
使用自定義指令格式化el-input
背景
使用element開發(fā)的過程中遇到循環(huán)的數(shù)據(jù)只能輸入數(shù)字,并且有不要小數(shù)點(diǎn),有需要小數(shù)點(diǎn)的
使用vue directive 進(jìn)行控制

開發(fā)
頁面使用方式 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對其監(jiān)聽失焦 當(dāng)輸入的不是數(shù)字時(shí),失焦后會變成0,沒有使用directive update方法,比較簡單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))
}
})
}})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決iView中時(shí)間控件選擇的時(shí)間總是少一天的問題
下面小編就為大家分享一篇解決iView中時(shí)間控件選擇的時(shí)間總是少一天的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
安裝@vue/cli 報(bào)錯(cuò)npm WARN deprecated request
這篇文章主要介紹了安裝@vue/cli 報(bào)錯(cuò)npm WARN deprecated request@2.88.2問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題
這篇文章主要介紹了解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Vue3項(xiàng)目中配置TypeScript和JavaScript的兼容
在Vue3開發(fā)中,常見的使用JavaScript(JS)編寫代碼,但也會有調(diào)整編寫語言使用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à)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

