u-city-select.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <u-popup :show="modelValue" mode="bottom" :popup="false" :mask="true" :closeable="true" :safe-area-inset-bottom="true"
  3. close-icon-color="#ffffff" :z-index="uZIndex" :maskCloseAble="maskCloseAble" @close="close">
  4. <u-tabs v-if="modelValue" :list="genTabsList" :scrollable="true" :current="tabsIndex" @change="tabsChange"
  5. ref="tabs" />
  6. <view class="area-box">
  7. <view class="u-flex" :class="{ 'change': isChange }">
  8. <view class="area-item">
  9. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  10. <scroll-view :scroll-y="true" style="height: 100%">
  11. <u-cell-group>
  12. <u-cell v-for="(item, index) in provinces" :title="item.label" :arrow="false" :index="index"
  13. :key="index" @click="provinceChange(index)">
  14. <template v-slot:right-icon>
  15. <u-icon v-if="isChooseP && province === index" size="17"
  16. name="checkbox-mark"></u-icon>
  17. </template>
  18. </u-cell>
  19. </u-cell-group>
  20. </scroll-view>
  21. </view>
  22. </view>
  23. <view class="area-item">
  24. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  25. <scroll-view :scroll-y="true" style="height: 100%">
  26. <u-cell-group v-if="isChooseP">
  27. <u-cell v-for="(item, index) in citys" :title="item.label" :arrow="false" :index="index"
  28. :key="index" @click="cityChange(index)">
  29. <template v-slot:right-icon>
  30. <u-icon v-if="isChooseC && city === index" size="17" name="checkbox-mark"></u-icon>
  31. </template>
  32. </u-cell>
  33. </u-cell-group>
  34. </scroll-view>
  35. </view>
  36. </view>
  37. <view class="area-item">
  38. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  39. <scroll-view :scroll-y="true" style="height: 100%">
  40. <u-cell-group v-if="isChooseC">
  41. <u-cell v-for="(item, index) in areas" :title="item.label" :arrow="false" :index="index"
  42. :key="index" @click="areaChange(index)">
  43. <template v-slot:right-icon>
  44. <u-icon v-if="isChooseA && area === index" size="17" name="checkbox-mark"></u-icon>
  45. </template>
  46. </u-cell>
  47. </u-cell-group>
  48. </scroll-view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </u-popup>
  54. </template>
  55. <script>
  56. import provinces from "./province.js";
  57. import citys from "./city.js";
  58. import areas from "./area.js";
  59. /**
  60. * city-select 省市区级联选择器
  61. * @property {String Number} z-index 弹出时的z-index值(默认1075)
  62. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
  63. * @property {String} default-region 默认选中的地区,中文形式
  64. * @property {String} default-code 默认选中的地区,编号形式
  65. */
  66. export default {
  67. name: 'u-city-select',
  68. props: {
  69. // 通过双向绑定控制组件的弹出与收起
  70. modelValue: {
  71. type: Boolean,
  72. default: false
  73. },
  74. // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
  75. defaultRegion: {
  76. type: Array,
  77. default() {
  78. return [];
  79. }
  80. },
  81. // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
  82. areaCode: {
  83. type: Array,
  84. default() {
  85. return [];
  86. }
  87. },
  88. // 是否允许通过点击遮罩关闭Picker
  89. maskCloseAble: {
  90. type: Boolean,
  91. default: true
  92. },
  93. // 弹出的z-index值
  94. zIndex: {
  95. type: [String, Number],
  96. default: 0
  97. }
  98. },
  99. data() {
  100. return {
  101. cityValue: "",
  102. isChooseP: false, //是否已经选择了省
  103. province: 0, //省级下标
  104. provinces: provinces,
  105. isChooseC: false, //是否已经选择了市
  106. city: 0, //市级下标
  107. citys: citys[0],
  108. isChooseA: false, //是否已经选择了区
  109. area: 0, //区级下标
  110. areas: areas[0][0],
  111. tabsIndex: 0,
  112. }
  113. },
  114. mounted() {
  115. this.init();
  116. },
  117. computed: {
  118. isChange() {
  119. return this.tabsIndex > 1;
  120. },
  121. genTabsList() {
  122. let tabsList = [{
  123. name: "请选择"
  124. }];
  125. if (this.isChooseP) {
  126. tabsList[0]['name'] = this.provinces[this.province]['label'];
  127. tabsList[1] = {
  128. name: "请选择"
  129. };
  130. }
  131. if (this.isChooseC) {
  132. tabsList[1]['name'] = this.citys[this.city]['label'];
  133. tabsList[2] = {
  134. name: "请选择"
  135. };
  136. }
  137. if (this.isChooseA) {
  138. tabsList[2]['name'] = this.areas[this.area]['label'];
  139. }
  140. return tabsList;
  141. },
  142. uZIndex() {
  143. // 如果用户有传递z-index值,优先使用
  144. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  145. }
  146. },
  147. emits: ['city-change'],
  148. methods: {
  149. init() {
  150. if (this.areaCode.length == 3) {
  151. this.setProvince("", this.areaCode[0]);
  152. this.setCity("", this.areaCode[1]);
  153. this.setArea("", this.areaCode[2]);
  154. } else if (this.defaultRegion.length == 3) {
  155. this.setProvince(this.defaultRegion[0], "");
  156. this.setCity(this.defaultRegion[1], "");
  157. this.setArea(this.defaultRegion[2], "");
  158. };
  159. },
  160. setProvince(label = "", value = "") {
  161. this.provinces.map((v, k) => {
  162. if (value ? v.value == value : v.label == label) {
  163. this.provinceChange(k);
  164. }
  165. })
  166. },
  167. setCity(label = "", value = "") {
  168. this.citys.map((v, k) => {
  169. if (value ? v.value == value : v.label == label) {
  170. this.cityChange(k);
  171. }
  172. })
  173. },
  174. setArea(label = "", value = "") {
  175. this.areas.map((v, k) => {
  176. if (value ? v.value == value : v.label == label) {
  177. this.isChooseA = true;
  178. this.area = k;
  179. }
  180. })
  181. },
  182. close() {
  183. this.$emit('update:modelValue', false);
  184. this.$emit('close');
  185. },
  186. tabsChange(value) {
  187. this.tabsIndex = value.index;
  188. },
  189. provinceChange(index) {
  190. this.isChooseP = true;
  191. this.isChooseC = false;
  192. this.isChooseA = false;
  193. this.province = index;
  194. this.citys = citys[index];
  195. this.tabsIndex = 1;
  196. },
  197. cityChange(index) {
  198. this.isChooseC = true;
  199. this.isChooseA = false;
  200. this.city = index;
  201. this.areas = areas[this.province][index];
  202. this.tabsIndex = 2;
  203. },
  204. areaChange(index) {
  205. this.isChooseA = true;
  206. this.area = index;
  207. let result = {};
  208. result.province = this.provinces[this.province];
  209. result.city = this.citys[this.city];
  210. result.area = this.areas[this.area];
  211. this.$emit('city-change', result);
  212. this.close();
  213. }
  214. }
  215. }
  216. </script>
  217. <style lang="scss">
  218. .area-box {
  219. width: 100%;
  220. overflow: hidden;
  221. height: 800rpx;
  222. >view {
  223. width: 150%;
  224. transition: transform 0.3s ease-in-out 0s;
  225. transform: translateX(0);
  226. &.change {
  227. transform: translateX(-33.3333333%);
  228. }
  229. }
  230. .area-item {
  231. width: 33.3333333%;
  232. height: 800rpx;
  233. }
  234. }
  235. </style>