anji-input.vue
5.17 KB
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
<template>
<div class="anji-input">
<el-input v-model="inputValue" type="number" :placeholder="placeholder" :disabled="disabled" clearable @change="change">
<template slot="append">
<el-dropdown @command="handleClick">
<el-button type="primary"> {{ systemUnit }}</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item v-for="item in SystemTimeConversionRadioGroup" :key="item.label" :command="item">{{ item.label }}</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</template>
</el-input>
</div>
</template>
<script>
export default {
props: {
value: null,
placeholder: null,
unit: String,
defaultUnit: String,
disabled: Boolean,
},
data() {
return {
inputValue: '',
input: '',
unitObj: {},
systemUnit: '', // 单位
systemConversion: '', // 转换单位
systemKeepPoint: '', // 保留小数位
systemRounding: '', // 取整方案
SystemTimeConversionRadioGroup: [], // 单位选择
}
},
watch: {
value: function (val, oldVal) {
this.echoInputValue(val)
},
},
mounted() {
this.getLocalStorage()
this.getSystemData()
this.echoInputValue(this.value)
},
methods: {
getSystemData() {
const defaultUnit = this.defaultUnit
this.systemKeepPoint = this.getSystemKeepPoint()
this.systemRounding = this.getSystemRounding()
this.SystemTimeConversionRadioGroup = this.getSystemTimeConversionRadioGroup()
if (defaultUnit == undefined) {
this.systemUnit = this.getSystemUnit()
this.systemConversion = this.getSystemConversion()
} else {
this.systemUnit = defaultUnit
this.systemConversion = this.SystemTimeConversionRadioGroup.find((item) => item.label == this.systemUnit)['value']
}
},
// 获取单位
getSystemUnit() {
let unit = ''
for (const key in this.unitObj) {
if (key.toLowerCase().indexOf('conversiontext') != -1) {
unit = this.unitObj[key]
}
}
return unit
},
// 获取转换关系
getSystemConversion() {
let conversion = ''
for (const key in this.unitObj) {
if (key.toLowerCase().indexOf('conversion') != -1 && key.toLowerCase().indexOf('conversiontext') == -1 && key.toLowerCase().indexOf('conversionradiogroup') == -1) {
conversion = this.unitObj[key]
}
}
return conversion
},
// 获取保留几位小数
getSystemKeepPoint() {
let keepPoint = ''
for (const key in this.unitObj) {
if (key.toLowerCase().indexOf('keeppoint') != -1) {
keepPoint = this.unitObj[key]
}
}
return keepPoint
},
// 获取怎么取整
getSystemRounding() {
let rounding = ''
for (const key in this.unitObj) {
if (key.toLowerCase().indexOf('pointrounding') != -1) {
rounding = this.unitObj[key]
}
}
return rounding
},
// 获取各类型的全部单位
getSystemTimeConversionRadioGroup() {
let radionGroup = ''
for (const key in this.unitObj) {
if (key.toLowerCase().indexOf('radiogroup') != -1) {
radionGroup = this.unitObj[key]
}
}
return radionGroup
},
handleClick(command) {
this.systemUnit = command.label
this.systemConversion = command.value
this.echoInputValue(this.value)
},
// 从本地获取数据
getLocalStorage() {
const localstorageData = this.getSettingByName('unit_conversion')
const unitObj = {}
for (const key in localstorageData) {
if (key.toLowerCase().indexOf(this.unit.toLowerCase()) != -1) {
unitObj[key] = localstorageData[key]
}
}
this.unitObj = unitObj
},
change(val) {
if (val === null || val === '') {
this.$emit('input', val)
this.$emit('change', val)
return
}
this.inputValue = val
if (this.systemRounding != undefined) {
let newVal
if (this.systemRounding == 'up') {
newVal = this.upFixed(val, this.systemKeepPoint)
}
if (this.systemRounding == 'down') {
newVal = this.downFixed(val, this.systemKeepPoint)
}
this.inputValue = newVal
const value = (newVal || val) * this.systemConversion
this.$emit('input', value)
this.$emit('change', value)
} else {
const value = val * this.systemConversion
this.inputValue = val
this.$emit('input', value)
this.$emit('change', value)
}
},
echoInputValue(val) {
if (val === undefined || val === '') {
this.inputValue = ''
} else {
this.inputValue = val / this.systemConversion
}
},
// 向上取整
upFixed(num, fix) {
return Math.ceil(num * Math.pow(10, fix)) / Math.pow(10, fix).toFixed(fix)
},
// 向下取整
downFixed(num, fix) {
return Math.floor(num * Math.pow(10, fix)) / Math.pow(10, fix).toFixed(fix)
},
},
}
</script>
<style lang="less" scoped>
/deep/.el-input__suffix {
padding: 0 6px;
}
.anji-input /deep/ .el-input__inner {
padding-right: 0 !important;
}
</style>