InputCurrencyCopy.vue 4.89 KB
Newer Older
liuxin committed
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
<template>
	<el-input :id="id" :class="{'highlight': highlight}" ref="form-item" v-model="model" v-bind="$attrs" v-on="$listeners" v-bind:disabled="isDisable" @blur="onBlur"
		@focus="focus" />
</template>

<script>
	export default {
		props: {
			value: {
				type: [String, Number],
				default: undefined
			},
			currency: {
				type: String,
				default: 'ZH'
			},
			precision: {
				type: Number,
				default: 2
			},
			max: {
				type: [Number, String],
				default: ''
			},
			min: {
				type: [Number, String],
				default: ''
			},
			disabled: {
				type: Boolean,
				default: false
			},
			id: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				isOnBlur: true
			}
		},
		computed: {
			model: {
				get() {
					return this.format(this.value)
				},
				set(newVal) {
					this.$emit('input', this.formatValue(newVal))
				}
			},
			mode() {
				return this.$store.state.Status.mode
			},
			highlight () {
				return this.$store.state.Status.highlights.indexOf(this.id) !== -1
			},
			isDisable: {
				get() {
					return this.mode === 'display' || this.disabled
				}
			},
		},
		methods: {
			onBlur() {
				this.isOnBlur = true
			},
			focus() {
				this.isOnBlur = false
			},
			format(value) {
				let max = this.max && this.max.toString && this.max.toString() || ''
				let min = this.min && this.min.toString && this.min.toString() || ''
				if (this.value !== null && this.value !== undefined && this.value.toString) {
					if (this.isOnBlur) {
						value = this.formatValue(value.toString())
						if (value === '') {
							this.model = ''
							return ''
						}
						if (max !== '' && this.compareNumber(value, max)) {
							value = max
						} else if (min !== '' && this.compareNumber(min, value)) {
							value = min
						}
						var array = value.split('.')
						var temp = ''
						while (array[0].length > 3) {
							temp = ',' + array[0].substring(array[0].length - 3, array[0].length) + temp
							array[0] = array[0].substring(0, array[0].length - 3)
						}
						temp = array[0] + temp
						if (this.currency === '392' || this.currency=== 'JPY') {
							this.model = temp
							return temp
						}
						if (array.length === 1) {
							temp += '.'
							for (var i = 0; i < this.precision; i++) {
								temp += '0'
							}
						} else if (array[1].length < this.precision) {
							temp += '.' + array[1]
							for (var i = array[1].length; i < this.precision; i++) {
								temp += '0'
							}
						} else {
							temp += '.' + array[1].substring(0, this.precision)
						}
						this.model = temp
						return temp
					} else {
						return this.formatValue(value)
					}
				}
				return this.value
			},
			formatValue(value) {
				if (value !== null && value !== undefined && value.toString) {
					var temp = value.toString().split(',').join('')
					if (temp.charAt(0) === '.')
						temp = '0' + temp
					if (temp.charAt(0) === '0' && temp.length > 1 && temp.charAt(1) !== '.')
						temp = temp.substring(1)
					if (this.currency === '392' || this.currency=== 'JPY') {
						var i = 0;
						while (i < temp.length) {
							if (!(temp.charAt(i) >= '0' && temp.charAt(i) <= '9'))
								break
							i++
						}
						temp = temp.substring(0, i)
					} else {
						var i = 0;
						while (i < temp.length) {
							if (!((temp.charAt(i) >= '0' && temp.charAt(i) <= '9') || temp.charAt(i) === '.'))
								break
							i++
						}
						temp = temp.substring(0, i)
						var array = temp.split('.')
						if (array.length > 1 && array[1].length > 0) {
							temp = array[0] + '.' + array[1].substring(0, this.precision)
						} else if (array.length > 1 && array[1].length === 0) {
							temp = array[0] + '.'
						} else {
							temp = array[0]
						}
					}
					return temp
				}
				return value
			},
			compareNumber(num1, num2) {
				var arr1 = num1.split('.')
				var arr2 = num2.split('.')
				if (this.compareInteger(arr1[0], arr2[0])) {
					return true
				} else if (arr1[0] === arr2[0]) {
					if (arr1.length > 1 && arr2.length > 1) {
						for (var i = arr2[1].length; i < arr1[1].length; i++) {
							arr2[1] += '0'
						}
						for (var i = arr1[1].length; i < arr2[1].length; i++) {
							arr1[1] += '0'
						}
						return arr1[1] > arr2[1]
					} else if (arr1.length > 1 && arr2.length === 1) {
						var str = ''
						for (var i = 0; i < arr1[1].length; i++) {
							str += '0'
						}
						return arr1[1] > str
					} else if (arr1.length === 1 && arr2.length > 1) {
						var str = ''
						for (var i = 0; i < arr2[1].length; i++) {
							str += '0'
						}
						return str > arr2[1]
					} else {
						return false
					}
				} else {
					return false
				}
			},
			compareInteger(num1, num2) {
				if (num1.length > num2.length) {
					return true
				} else if (num1.length < num2.length) {
					return false
				} else {
					return num1 > num2
				}
			}

		}
	}
</script>

<style>
.el-input.highlight .el-input__inner{
  border-color: red;
}
</style>