CnumberBox.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view class="main">
  3. <uni-icons type="minus-filled" size="20" color="#A67A54" @click="numReduce"></uni-icons>
  4. <!-- <text class="buy-select">{{number}}</text> -->
  5. <u-input
  6. v-model="number"
  7. :type="isInt?'number' : 'digit'"
  8. height="40"
  9. :placeholder="placeholder"
  10. :clearable="false"
  11. :disabled="isDisabled"
  12. input-align="center"
  13. :custom-style="{width:'45px'}"
  14. @input="onKeyInput"/>
  15. <!-- <input class="uni-input" v-model="number" :type="isInt?'number' : 'digit'" :disabled="isDisabled" :placeholder="placeholder" @input="onKeyInput" > -->
  16. <uni-icons type="plus-filled" size="20" color="#A67A54" @click="numPlus"></uni-icons>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. isDisabled: {
  23. default: false
  24. },
  25. isInt: {
  26. default: true
  27. },
  28. placeholder: {
  29. default: ''
  30. },
  31. step: {
  32. default: 1
  33. },
  34. minNum: {
  35. default: 0.01
  36. }
  37. },
  38. data() {
  39. return {
  40. number: 1,
  41. }
  42. },
  43. methods: {
  44. onKeyInput(event) {
  45. // uniapp的input用这个
  46. // let i = event.target.value
  47. // u-view的input用这个
  48. let i = event
  49. let num = i.charAt(i.length-1)
  50. var reg=new RegExp("^[0-9]*$")
  51. if(!reg.test(num)) {
  52. this.number = i.slice(0,-1)
  53. } else {
  54. this.number = i
  55. }
  56. this.$emit('getNum',this.number);
  57. },
  58. numPlus() {
  59. this.number = Number(this.number) + Number(this.step)
  60. this.$emit('getNum',this.number);
  61. },
  62. numReduce() {
  63. if ((Number(this.number) - Number(this.step)) >= Number(this.minNum)) {
  64. this.number = Number(this.number) - Number(this.step)
  65. }
  66. this.$emit('getNum',this.number);
  67. }
  68. }
  69. }
  70. </script>
  71. <style>
  72. </style>
  73. <style lang="less" scoped>
  74. .main {
  75. display: flex;
  76. align-items: center;
  77. justify-content: flex-end;
  78. .uni-input {
  79. height: 40px;
  80. width: 45px;
  81. text-align: center;
  82. }
  83. }
  84. </style>