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