en-city.vue 4.2 KB

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