yaozc
2023-07-07 c752e33fe402e2179e7e6795f567d41c044ca3c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<template>
  <div>
    <!-- 金额标准输入框 -->
    <el-input
      ref="value"
      v-model="value"
      :style="{width: width + 'px'}"
      :class="{'web-display-none': showDisplay, 'web-display-inline': !showDisplay}"
      :disabled="disabled"
      @blur="moneyBlur($event.target.value)"
    />
    <!-- 金额格式输入框 -->
    <el-input
      v-model="valueStr"
      :style="{width: width + 'px'}"
      :class="{'web-display-none': !showDisplay, 'web-display-inline': showDisplay}"
      :disabled="disabled"
      @focus="moneyFocus($event.target.value)"
    />
  </div>
</template>
 
<script>
export default {
  name: 'AssemblyMoneyInput',
  model: {
    // 指向props的参数名
    prop: 'modelVal',
    // 事件名称
    event: 'change'
  },
  props: {
    modelVal: {
      type: Number,
      default() {
        return null
      }
    },
    width: {
      type: Number,
      default() {
        return 200
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      // 金额输入框显示状态
      showDisplay: false,
      // 金额标准输入框数据
      value: null,
      // 金额格式输入框数据
      valueStr: null
    }
  },
  watch: {
    // 监听金额标准输入框数据变化:赋值给modelVal
    value(val) {
      this.$emit('change', Number(val))
      // 回调钩子
      this.$emit('afterChange')
    }
  },
  mounted() {
    // 金额标准输入框初始化
    this.value = this.modelVal
    // 金额输入框显示初始化
    this.showDisplay = false
    // 判断是否有初始金额
    if (this.value != null) {
      // 金额标准输入框失去焦点触发
      this.moneyBlur(this.value)
    }
  },
  methods: {
    // 金额标准输入框失去焦点触发
    moneyBlur(el) {
      // 金额输入框显示状态切换
      this.showDisplay = true
      // 金额格式化
      this.getInputValue(el)
      // 函数回调
      this.$emit('blur')
    },
    // 金额格式输入框获取焦点触发
    moneyFocus(el) {
      // 金额输入框显示状态切换
      this.showDisplay = false
      // 自动获取焦点
      this.$nextTick(() => {
        this.$refs.value.focus()
      })
    },
    // 金额格式化
    getInputValue(el) {
      // 金额
      const inputVal = el.toString() || ''
      // 金额添加千分位(金额去除千分位(金额))
      this.comdify(this.delcommafy(inputVal))
    },
    // 金额去除千分位
    delcommafy(num) {
      if (!num) return num
      num = num.toString()
      num = num.replace(/,/gi, '')
      return num
    },
    // 金额添加千分位
    comdify(n) {
      if (!n) return n
      const str = n.split('.')
      const re = /\d{1,3}(?=(\d{3})+$)/g
      const n1 = str[0].replace(re, '$&,')
      // 金额格式输入框数据
      this.valueStr = str.length > 1 && str[1].length > 2 ? `${n1}.${str[1].substring(0, 2)}` : str.length > 1 && str[1].length > 1 ? `${n1}.${str[1]}` : str.length > 1 && str[1].length ? `${n1}.${str[1]}0` : `${n1}.00`
      // 金额标准输入框数据
      this.value = str.length > 1 && str[1].length > 2 ? Number(`${str[0]}.${str[1].substring(0, 2)}`) : str.length > 1 && str[1].length > 1 ? Number(`${str[0]}.${str[1]}`) : str.length > 1 && str[1].length ? Number(`${str[0]}.${str[1]}0`) : Number(`${str[0]}.00`)
    }
  }
}
</script>
 
<style scoped>
.web-display-none {
  display: none !important;
}
.web-display-inline {
  display: inline-block !important;
}
</style>