en-select.vue 3.7 KB

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