en-select.vue 4.1 KB

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