InputNumber.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <div>
  3. <div class="input-number-range" :class="{ 'is-disabled': disabled }">
  4. <div class="flex">
  5. <div class="from">
  6. <el-input
  7. ref="input_from"
  8. oninput="value=value.replace(/[^\d]/g,'')"
  9. v-model="userInputForm"
  10. :disabled="disabled"
  11. placeholder="最小值"
  12. @blur="handleBlurFrom"
  13. @focus="handleFocusFrom"
  14. @input="handleInputFrom"
  15. @change="handleInputChangeFrom"
  16. ></el-input>
  17. </div>
  18. <div class="center">
  19. <span>至</span>
  20. </div>
  21. <div class="to">
  22. <el-input
  23. ref="input_to"
  24. oninput="value=value.replace(/[^\d]/g,'')"
  25. v-model="userInputTo"
  26. :disabled="disabled"
  27. placeholder="最大值"
  28. @blur="handleBlurTo"
  29. @focus="handleFocusTo"
  30. @input="handleInputTo"
  31. @change="handleInputChangeTo"
  32. ></el-input>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </template>
  38. <script>
  39. export default {
  40. name: 'InputNumber',
  41. props: {
  42. // 初始化范围
  43. value: { required: true },
  44. // 是否禁用
  45. disabled: {
  46. type: Boolean,
  47. default: false
  48. },
  49. // 精度参数
  50. precision: {
  51. type: Number,
  52. default: 0,
  53. validator (val) {
  54. return val >= 0 && val === parseInt(val, 10)
  55. }
  56. }
  57. },
  58. data () {
  59. return {
  60. userInputForm: null,
  61. userInputTo: null
  62. }
  63. },
  64. watch: {
  65. value: {
  66. immediate: true,
  67. handler (value) {
  68. /** 初始化范围 */
  69. if (value instanceof Array && this.precision !== undefined) {
  70. this.userInputForm = typeof value[0] === 'number' ? value[0] : null
  71. this.userInputTo = typeof value[1] === 'number' ? value[1] : null
  72. }
  73. }
  74. }
  75. },
  76. methods: {
  77. // 根据精度保留数字
  78. toPrecision (num, precision) {
  79. if (precision === undefined) precision = 0
  80. return parseFloat(
  81. Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision)
  82. )
  83. },
  84. handleBlurFrom (event) {
  85. this.$emit('blurfrom', event)
  86. },
  87. handleFocusFrom (event) {
  88. this.$emit('focusfrom', event)
  89. },
  90. handleBlurTo (event) {
  91. this.$emit('blurto', event)
  92. },
  93. handleFocusTo (event) {
  94. this.$emit('focusto', event)
  95. },
  96. handleInputFrom (value) {
  97. this.$emit('inputfrom', value)
  98. },
  99. handleInputTo (value) {
  100. this.$emit('inputto', value)
  101. },
  102. // from输入框change事件
  103. handleInputChangeFrom (value) {
  104. // 如果是非数字空返回null
  105. if (isNaN(value) || value === '') {
  106. this.$emit('input', [null, this.userInputTo])
  107. this.$emit('changefrom', this.userInputForm)
  108. return
  109. }
  110. // 初始化数字精度
  111. this.userInputForm = this.setPrecisionValue(value)
  112. // 如果from > to 将from值替换成to
  113. if (typeof this.userInputTo === 'number') {
  114. this.userInputForm =
  115. parseFloat(this.userInputForm) <= parseFloat(this.userInputTo)
  116. ? this.userInputForm
  117. : this.userInputTo
  118. }
  119. this.$emit('input', [this.userInputForm, this.userInputTo])
  120. this.$emit('changefrom', this.userInputForm)
  121. },
  122. // to输入框change事件
  123. handleInputChangeTo (value) {
  124. // 如果是非数字空返回null
  125. if (isNaN(value) || value === '') {
  126. this.$emit('input', [this.userInputForm, null])
  127. this.$emit('changefrom', this.userInputTo)
  128. return
  129. }
  130. // 初始化数字精度
  131. this.userInputTo = this.setPrecisionValue(value)
  132. // 如果to < tfrom 将to值替换成from
  133. if (typeof this.userInputForm === 'number') {
  134. this.userInputTo =
  135. parseFloat(this.userInputTo) >= parseFloat(this.userInputForm)
  136. ? this.userInputTo
  137. : this.userInputForm
  138. }
  139. this.$emit('input', [this.userInputForm, this.userInputTo])
  140. this.$emit('changeto', this.userInputTo)
  141. },
  142. // 设置成精度数字
  143. setPrecisionValue (value) {
  144. if (this.precision !== undefined) {
  145. const val = this.toPrecision(value, this.precision)
  146. return val
  147. }
  148. return null
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. /*// 取消element原有的input框样式*/
  155. ::v-deep .el-input--mini .el-input__inner {
  156. border: 0px;
  157. margin: 0;
  158. padding: 0 15px;
  159. background-color: transparent;
  160. }
  161. .input-number-range {
  162. background-color: #fff;
  163. /*border: 1px solid #dcdfe6;*/
  164. border-radius: 4px;
  165. }
  166. .flex {
  167. display: flex;
  168. flex-direction: row;
  169. width: 100%;
  170. justify-content: center;
  171. align-items: center;
  172. .center {
  173. margin-top: 1px;
  174. }
  175. }
  176. .is-disabled {
  177. background-color: #eef0f6;
  178. border-color: #e4e7ed;
  179. color: #c0c4cc;
  180. cursor: not-allowed;
  181. }
  182. </style>