en-select.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. localData: {
  22. type: Array,
  23. default: []
  24. },
  25. label: {
  26. type: String,
  27. default: '标题'
  28. },
  29. placeholder: {
  30. type: String,
  31. default: '请选择'
  32. },
  33. disabled: {
  34. type: Boolean,
  35. default: false
  36. },
  37. name: {
  38. type: String,
  39. default: 'text'
  40. },
  41. valueType: {
  42. type: String,
  43. default: '1'
  44. },
  45. value: {
  46. default: ''
  47. }
  48. },
  49. data() {
  50. return {
  51. inputValue: [],
  52. optionName: '23432432',
  53. labelWidth: 0,
  54. showPicker:false
  55. }
  56. },
  57. components: {},
  58. mounted() {
  59. this.setValue()
  60. this.setLabelWidth()
  61. },
  62. watch: {
  63. 'value': function () {
  64. this.setValue()
  65. },
  66. 'inputValue': function () {
  67. console.log(this.inputValue)
  68. if(this.valueType==='1'){
  69. this.$emit('input', this.inputValue.join(','))
  70. }else {
  71. this.$emit('input', this.inputValue)
  72. }
  73. }
  74. },
  75. methods: {
  76. setValue(){
  77. let value=this.value
  78. if(typeof value==='string'){
  79. value=value.split(',')
  80. }
  81. if(this.inputValue!==value){
  82. this.inputValue=value
  83. this.optionName = "";
  84. this.localData.forEach((one)=>{
  85. if(one.value+''===this.inputValue[0]+''){
  86. this.optionName += one.text + " ";
  87. if(one.children){
  88. one.children.forEach((two)=>{
  89. if(two.value+''===this.inputValue[1]+''){
  90. this.optionName += two.text + " ";
  91. if(two.children){
  92. two.children.forEach((three)=>{
  93. if(three.value+''===this.inputValue[2]+''){
  94. this.optionName += three.text + " ";
  95. }
  96. })
  97. }
  98. }
  99. })
  100. }
  101. }
  102. })
  103. }
  104. },
  105. setPopupClosed() {
  106. this.showPicker = false;
  107. },
  108. showPickerObj() {
  109. this.$refs.pickerObj.show();
  110. this.showPicker = true;
  111. },
  112. pickerChange(data) {
  113. this.optionName = "";
  114. this.inputValue = [];
  115. data.detail.value.forEach((item) => {
  116. this.optionName += item.text + " ";
  117. this.inputValue.push(item.value)
  118. });
  119. this.showPicker = false;
  120. },
  121. setLabelWidth() {
  122. let differenceNum = 4 - this.label.length;
  123. if (differenceNum === 2) {
  124. this.labelWidth = '2em'
  125. } else if (differenceNum === 1) {
  126. this.labelWidth = '0.5em'
  127. }
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. @import url("../../static/css/en-common.css");
  134. .box {
  135. .input-box {
  136. display: flex;
  137. align-items: center;
  138. justify-content: space-between;
  139. .input-box-left {
  140. width: 210rpx;
  141. font-size: 32rpx;
  142. color: #333333;
  143. }
  144. .input-box-right {
  145. width: 100%;
  146. text {
  147. font-size: 32rpx;
  148. color: #333;
  149. }
  150. .iconfont {
  151. float: right;
  152. }
  153. .no-option {
  154. color: #999999;
  155. }
  156. }
  157. }
  158. }
  159. </style>