u-city-select.vue 6.6 KB

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