vue指令:限制輸入數字,可配置最大值最小值及小數點位數

效果

在這裏插入圖片描述

代碼

在這裏插入圖片描述

  • onlyNumber.js 文件

export default {
  inserted (el, vDir, vNode) {
      // vDir.value 有指令的參數
    let content
      // 設置輸入框的值,觸發input事件,改變v-model綁定的值
    const setVal = val => {
      if (vNode.componentInstance) {
          // 如果是自定義組件就觸發自定義組件的input事件
        vNode.componentInstance.$emit('input', val)
      } else {
          // 如果是原生組件就觸發原生組件的input事件
        el.value = val
        el.dispatchEvent(new Event('input'))
      }
    }
      // 按鍵按下=>只允許輸入 數字/小數點/減號
    el.addEventListener('keypress', event => {
      const e = event || window.event
      const inputKey = String.fromCharCode(typeof e.charCode === 'number' ? e.charCode : e.keyCode)
      const re = /\d|\.|-/
      content = e.target.value
        // 定義方法,阻止輸入
      function preventInput () {
        if (e.preventDefault) {
          e.preventDefault()
        } else {
          e.returnValue = false
        }
      }
      if (!re.test(inputKey) && !e.ctrlKey) {
        preventInput()
      } else if (content.indexOf('.') > 0 && inputKey === '.') {
          // 已有小數點,再次輸入小數點
        preventInput()
      }
    })
      // 按鍵彈起=>並限制最大最小
    el.addEventListener('keyup', event => {
      const e = event || window.event
      content = parseFloat(e.target.value)
      if (!content) {
        content = 0.00
      }
      let argMax = ''
      let argMin = ''
      if (vDir.value) {
        argMax = parseFloat(vDir.value.max)
        argMin = parseFloat(vDir.value.min)
      }
      if (argMax !== undefined && content > argMax) {
        setVal(argMax)
        content = argMax
      }
      if (argMin !== undefined && content < argMin) {
        setVal(argMin)
        content = argMin
      }
    })
      // 失去焦點=>保留指定位小數
    el.addEventListener('focusout', event => { // 此處會在 el-input 的 @change 後執行
      const e = event || window.event
      content = parseFloat(e.target.value)
      if (!content) {
        content = 0.00
      }
      let argPrecision = 0// 默認保留至整數
      if (vDir.value.precision) {
        argPrecision = parseFloat(vDir.value.precision)
      }
      e.target.value = content.toFixed(argPrecision)
      setVal(e.target.value)
        // -- callback寫法1
        // vNode.data.model.callback = ()=>{
        //     e.target.value = content.toFixed(argPrecision)
        // }
        // vNode.data.model.callback();
        // -- callback 寫法2
        // vNode.data.model.callback(
        //     e.target.value = content.toFixed(argPrecision)
        // )
    })
  }
}

  • index.js
import onlyNumber from './onlyNumber'
const install = Vue => {
  Vue.directive('only-number', onlyNumber)
}
/*
  Vue.use( plugin )
  安裝 Vue.js 插件。如果插件是一個對象,必須提供 install 方法。
  如果插件是一個函數,它會被作爲 install 方法。install 方法調用時,會將 Vue 作爲參數傳入。
  該方法需要在調用 new Vue() 之前被調用。
  當 install 方法被同一個插件多次調用,插件將只會被安裝一次。
  --------------------------------------------------------
  調用示例
  最大兩位小數
  <el-input
    v-only-number="{ min: 0, max: 100, precision: 2 }"
    v-model.number="price"
  />
  只允許輸入整數
  <input
    v-only-number="{ min: -10, max: 100, precision: 0 }"
    v-model.number="price"
  />
*/

if (window.Vue) {
  window['onlyNumber'] = onlyNumber
  Vue.use(install); // eslint-disable-line
}

onlyNumber.install = install
export default onlyNumber

  • main.js
import onlyNumber from '@/script/onlyNumber'
Vue.use(onlyNumber)

使用

 <el-input v-model="value1"  v-only-number="{precision: 0}"  placeholder="請輸入" ></el-input>
 <el-input v-model="value1"  v-only-number="{ min: 0, max: 100, precision: 2}"  placeholder="請輸入" ></el-input>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章