en-select.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="box">
  3. <view class="input-box flex-shrink0" @click="showPickerObj">
  4. <view class="input-box-left size-28" :style="{'letter-spacing':labelWidth}">
  5. {{ label }}
  6. </view>
  7. <view class="input-box-right row-c">
  8. <text class="size-28 p-r10"
  9. :class="{'no-option':!optionName}">{{ optionName ? optionName : placeholder }}</text>
  10. <!-- <text class="iconfont">&#xe62b;</text> -->
  11. <uni-icons type="forward" size="18" color="#D8D8D8"></uni-icons>
  12. </view>
  13. </view>
  14. <uni-data-picker :popup-title="'选择'+label" :localdata="localData" ref="pickerObj" v-show="showPicker"
  15. @change="pickerChange" :border="false" :clear-icon="false" @popupclosed="setPopupClosed">
  16. </uni-data-picker>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'en-select',
  22. props: {
  23. map: {
  24. default: {}
  25. },
  26. localData: {
  27. default: []
  28. },
  29. label: {
  30. type: String,
  31. default: '标题'
  32. },
  33. placeholder: {
  34. type: String,
  35. default: '请选择'
  36. },
  37. disabled: {
  38. type: Boolean,
  39. default: false
  40. },
  41. name: {
  42. type: String,
  43. default: 'text'
  44. },
  45. valueType: {
  46. type: String,
  47. default: '1'
  48. },
  49. value: {
  50. default: ''
  51. }
  52. },
  53. data() {
  54. return {
  55. inputValue: [],
  56. optionName: '',
  57. labelWidth: 0,
  58. showPicker: false
  59. }
  60. },
  61. components: {},
  62. mounted() {
  63. this.setValue()
  64. this.setLabelWidth()
  65. },
  66. watch: {
  67. 'value': function() {
  68. this.setValue()
  69. },
  70. 'inputValue': function() {
  71. if (this.valueType === '1') {
  72. this.$emit('input', this.inputValue.join(','))
  73. } else {
  74. console.log('this.inputValue', this.inputValue)
  75. this.$emit('input', this.inputValue)
  76. }
  77. },
  78. 'localData': function() {
  79. this.setValue()
  80. }
  81. },
  82. methods: {
  83. setValue() {
  84. let value = this.value
  85. if (typeof value === 'number') {
  86. value += '';
  87. }
  88. if (!value) {
  89. value = []
  90. } else if (typeof value === 'string') {
  91. value = value.split(',')
  92. }
  93. if (value && (this.inputValue !== value || this.optionName === '')) {
  94. this.inputValue = value
  95. this.optionName = "";
  96. this.localData.forEach((one) => {
  97. if (this.inputValue[0] && one.value + '' === this.inputValue[0] + '') {
  98. this.optionName += one.text + " ";
  99. if (one.children) {
  100. one.children.forEach((two) => {
  101. if (this.inputValue[1] && two.value + '' === this.inputValue[1] + '') {
  102. this.optionName += two.text + " ";
  103. if (two.children) {
  104. two.children.forEach((three) => {
  105. if (this.inputValue[2] && three.value + '' === this
  106. .inputValue[2] + '') {
  107. this.optionName += three.text + " ";
  108. }
  109. })
  110. }
  111. }
  112. })
  113. }
  114. }
  115. })
  116. }
  117. },
  118. setPopupClosed() {
  119. this.showPicker = false;
  120. },
  121. showPickerObj() {
  122. this.$refs.pickerObj.show();
  123. this.showPicker = true;
  124. },
  125. pickerChange(data) {
  126. this.optionName = "";
  127. this.inputValue = [];
  128. console.log(data)
  129. data.detail.value.forEach((item) => {
  130. this.optionName += item.text + " ";
  131. this.inputValue.push(item.value)
  132. });
  133. this.showPicker = false;
  134. },
  135. setLabelWidth() {
  136. let differenceNum = 4 - this.label.length;
  137. if (differenceNum === 2) {
  138. this.labelWidth = '2em'
  139. } else if (differenceNum === 1) {
  140. this.labelWidth = '0.5em'
  141. }
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. @import url("../../static/css/en-common.css");
  148. ::v-deep .selected-item-active {
  149. border-bottom: 2px solid #FF0000 !important;
  150. }
  151. .box {
  152. .input-box {
  153. display: flex;
  154. align-items: center;
  155. justify-content: space-between;
  156. .input-box-left {
  157. color: #333333;
  158. }
  159. .input-box-right {
  160. text-align: right;
  161. text {
  162. font-size: 28rpx;
  163. color: #333;
  164. }
  165. .iconfont {
  166. float: right;
  167. }
  168. .no-option {
  169. color: #999999;
  170. }
  171. }
  172. }
  173. }
  174. </style>