u-city-select.vue 6.2 KB

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