en-select.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-model="inputValue" 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: '',
  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("1111111111111",this.valueType, this.inputValue);
  68. if(this.valueType==='1'){
  69. console.log(this.inputValue.join(','));
  70. this.$emit('input', this.inputValue.join(','))
  71. // this.$emit('upValue',this.inputValue.join(','))
  72. }else {
  73. this.$emit('input', this.inputValue)
  74. }
  75. }
  76. },
  77. methods: {
  78. setValue(){
  79. let value=this.value
  80. if(!value){
  81. value=[]
  82. }else if(typeof value==='string'){
  83. value=value.split(',')
  84. }
  85. if(this.inputValue!==value){
  86. this.inputValue=value
  87. this.optionName = "";
  88. this.localData.forEach((one)=>{
  89. if(one.value+''===this.inputValue[0]+''){
  90. this.optionName += one.text + " ";
  91. if(one.children){
  92. one.children.forEach((two)=>{
  93. if(two.value+''===this.inputValue[1]+''){
  94. this.optionName += two.text + " ";
  95. if(two.children){
  96. two.children.forEach((three)=>{
  97. if(three.value+''===this.inputValue[2]+''){
  98. this.optionName += three.text + " ";
  99. }
  100. })
  101. }
  102. }
  103. })
  104. }
  105. }
  106. })
  107. }
  108. },
  109. setPopupClosed() {
  110. this.showPicker = false;
  111. },
  112. showPickerObj() {
  113. this.$refs.pickerObj.show();
  114. this.showPicker = true;
  115. },
  116. pickerChange(data) {
  117. this.optionName = "";
  118. this.inputValue = [];
  119. data.detail.value.forEach((item) => {
  120. this.optionName += item.text + " ";
  121. this.inputValue.push(item.value)
  122. });
  123. this.showPicker = false;
  124. },
  125. setLabelWidth() {
  126. let differenceNum = 4 - this.label.length;
  127. if (differenceNum === 2) {
  128. this.labelWidth = '2em'
  129. } else if (differenceNum === 1) {
  130. this.labelWidth = '0.5em'
  131. }
  132. }
  133. }
  134. }
  135. </script>
  136. <style lang="scss" scoped>
  137. @import url("../../static/css/en-common.css");
  138. ::v-deep .selected-item-active{
  139. border-bottom: 2px solid #FF0000 !important;
  140. }
  141. .box {
  142. .input-box {
  143. display: flex;
  144. align-items: center;
  145. justify-content: space-between;
  146. .input-box-left {
  147. width: 210rpx;
  148. font-size: 32rpx;
  149. color: #333333;
  150. }
  151. .input-box-right {
  152. width: 100%;
  153. text {
  154. font-size: 32rpx;
  155. color: #333;
  156. }
  157. .iconfont {
  158. float: right;
  159. }
  160. .no-option {
  161. color: #999999;
  162. }
  163. }
  164. }
  165. }
  166. </style>