u-city-select.vue 6.4 KB

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