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

開發(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>
因為用的是element 的el-input ,組件input外層包著一層div所以要使用const element = el.getElementsByTagName('input')[0]獲取 input對其監(jiān)聽失焦 當輸入的不是數(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 // 初始化設置
element.value = Number(element.value ).toFixed(len) // 失焦時候格式化
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue動態(tài)加載圖片在跨域時無法顯示的問題及解決方法
這篇文章主要介紹了解決VUE動態(tài)加載圖片在跨域時無法顯示的問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Vue3?Watch踩坑實戰(zhàn)之watch監(jiān)聽無效
Vue.js中的watch選項用于監(jiān)聽Vue實例上某個特定的數(shù)據(jù)變化,下面這篇文章主要給大家介紹了關于Vue3?Watch踩坑實戰(zhàn)之watch監(jiān)聽無效的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
詳解Vue中數(shù)據(jù)可視化詞云展示與詞云的生成
數(shù)據(jù)可視化是現(xiàn)代Web應用程序中的一個重要組成部分,詞云是一種非常流行的數(shù)據(jù)可視化形式,可以用來展示文本數(shù)據(jù)中的主題和關鍵字,本文我們將介紹如何在Vue中使用詞云庫進行數(shù)據(jù)可視化詞云展示和詞云生成,需要的可以參考一下2023-06-06

