elementel-inputdirective數(shù)字進(jìn)行控制的示例分析

這篇文章主要為大家展示了“element el-input directive數(shù)字進(jìn)行控制的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“element el-input directive數(shù)字進(jìn)行控制的示例分析”這篇文章吧。

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的筠連網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

使用自定義指令格式化el-input

背景

使用element開(kāi)發(fā)的過(guò)程中遇到循環(huán)的數(shù)據(jù)只能輸入數(shù)字,并且有不要小數(shù)點(diǎn),有需要小數(shù)點(diǎn)的

使用vue directive 進(jìn)行控制

element el-input directive數(shù)字進(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)) 
   }  
   }) 
 }})

以上是“element el-input directive數(shù)字進(jìn)行控制的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)題目:elementel-inputdirective數(shù)字進(jìn)行控制的示例分析
分享網(wǎng)址:http://muchs.cn/article8/ijdsip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站排名、網(wǎng)站營(yíng)銷(xiāo)企業(yè)建站、小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)