vue输入框数字千分位符封装转换

<template>

  <div :class="classComputed">

    <input

      ref="input"

      style="text-align: right;"

      class="el-input__inner"

      :placeholder="placeholder"

      :value="formatValue"

      :disabled="disabled"

      @input="updateValue($event.target.value)"

      @blur="onBlur"

      @focus="selectAll"

    >

  </div>

</template>

<script>

import accounting from 'accounting'

export default {

  props: {

    value: {

      type: [String, Number],

      default: '',

      desc: '数值'

    },

    disabled: {

      type: Boolean,

      default: false

    },

    symbol: {

      type: String,

      default: '',

      desc: '货币标识符'

    },

    placeholder: {

      type: String,

      default: '',

      desc: '货币标识符'

    },

    precision: {

      type: Number,

      default: 2,

      desc: '小数位'

    },

    min: {

      type: [String, Number],

      default: '',

      desc: '最小值'

    },

    elvalue: [String, Number]

  },

  data () {

    return {

      focused: false

    }

  },

  computed: {

    classComputed () {

      let str = 'el-input el-input--mini'

      if (this.disabled) {

        str = `${this.className} ${str} is-disabled`

      }

      return str

    },

    formatValue () {

      if (this.focused) {

        let formatValue

        if (this.value === '' || this.value === undefined) {

          formatValue = ''

        } else if (this.value === '-') {

          formatValue = '-'

        } else {

          formatValue = accounting.unformat(this.value)

        }

        return formatValue

      } else {

        if (this.value === 0) {

          return accounting.formatMoney(this.value, this.symbol, this.precision)

        } else if (

          this.value === '' ||

          this.value === null ||

          this.value === undefined

        ) {

          return ''

        } else {

          return accounting.formatMoney(this.value, this.symbol, this.precision)

        }

      }

    }

  },

  methods: {

    updateValue (value) {

      let formatValue

      if (value === '' || value === undefined) {

        formatValue = ''

      } else if (value === '-') {

        formatValue = '-'

      } else {

        formatValue = accounting.unformat(value)

      }

      this.$emit('input', formatValue)

    },

    onBlur () {

      this.focused = false

      this.$emit('blur', event)

      this.dispatch('ElFormItem', 'el.form.blur', [this.value])

    },

    selectAll (event) {

      this.focused = true

      setTimeout(() => {

        event.target.select()

      }, 0)

    },

    dispatch (componentName, eventName, params) {

      var parent = this.$parent || this.$root

      var name = parent.$options.componentName

      while (parent && (!name || name !== componentName)) {

        parent = parent.$parent

        if (parent) {

          name = parent.$options.componentName

        }

      }

      if (parent) {

        parent.$emit.apply(parent, [eventName].concat(params))

      }

    }

  }

}

</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值