u-city-select-province.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 == 2) {
  147. this.setProvince(this.defaultRegion[0], "");
  148. this.setCity(this.defaultRegion[1], "");
  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>