当前位置:   article > 正文

uniapp省市区3级联动组件_uniapp省市区三级联动插件

uniapp省市区三级联动插件

1、组件代码region.vue

  1. <template>
  2. <view>
  3. <!-- 省市区3级联动组件 -->
  4. <picker mode="multiSelector" :value="multiIndex" :range="multiArray" @change="handleValueChange"
  5. @columnchange="handleColumnChange">
  6. <slot></slot>
  7. </picker>
  8. </view>
  9. </template>
  10. <script>
  11. // 省市区数据
  12. const regionData = require('./regions.json')
  13. // const regionData = uni.getStorageSync('regionData');
  14. export default {
  15. data() {
  16. return {
  17. cityArr: regionData[0].child,
  18. districtArr: regionData[0].child[0].child,
  19. multiIndex: [0, 0, 0],
  20. isInitMultiArray: true,
  21. }
  22. },
  23. methods: {
  24. // 某一列的值改变时触发 columnchange 事件
  25. handleColumnChange(e) {
  26. // console.log(e);
  27. let _this = this;
  28. _this.isInitMultiArray = false;
  29. let col = e.detail.column;
  30. let row = e.detail.value;
  31. _this.multiIndex[col] = row;
  32. try {
  33. switch (col) {
  34. case 0:
  35. if (regionData[_this.multiIndex[0]].child.length == 0) {
  36. _this.cityArr = _this.districtArr = [regionData[_this.multiIndex[0]]]
  37. break;
  38. }
  39. _this.cityArr = regionData[_this.multiIndex[0]].child
  40. _this.districtArr = regionData[_this.multiIndex[0]].child[_this.multiIndex[1]].child
  41. break;
  42. case 1:
  43. _this.districtArr = regionData[_this.multiIndex[0]].child[_this.multiIndex[1]].child
  44. break;
  45. case 2:
  46. break;
  47. }
  48. } catch (e) {
  49. // console.log(e);
  50. _this.districtArr = regionData[_this.multiIndex[0]].child[0].child
  51. }
  52. },
  53. // value 改变时触发 change 事件
  54. handleValueChange(e) {
  55. // console.log(e);
  56. // 结构赋值
  57. let _this = this;
  58. let [index0, index1, index2] = e.detail.value;
  59. let [arr0, arr1, arr2] = _this.pickedArr;
  60. let address = [arr0[index0], arr1[index1], arr2[index2]];
  61. // console.log(address);
  62. _this.$emit('getRegion', address)
  63. },
  64. },
  65. computed: {
  66. multiArray() {
  67. return this.pickedArr.map(arr => arr.map(item => item.name))
  68. },
  69. pickedArr() {
  70. // 进行初始化
  71. if (this.isInitMultiArray) {
  72. return [
  73. regionData,
  74. regionData[0].child,
  75. regionData[0].child[0].child
  76. ]
  77. }
  78. return [regionData, this.cityArr, this.districtArr];
  79. }
  80. },
  81. }
  82. </script>

2、组件的使用

  1. <template>
  2. <view>
  3. <pickerRegion @getRegion="regionChange">
  4. <view class="pickerCon">
  5. <text v-if="address">{{address}}</text>
  6. <text style="color: #999;" v-else>请选择所在地区</text>
  7. <u-icon name="arrow-right" color="#ccc" size="28"></u-icon>
  8. </view>
  9. </pickerRegion>
  10. </view>
  11. </template>
  12. <script>
  13. import pickerRegion from '@/components/region/region.vue'
  14. export default {
  15. components: {
  16. pickerRegion
  17. },
  18. data() {
  19. return {
  20. // 地区
  21. address: '',
  22. }
  23. },
  24. onLoad(option) {},
  25. onShow() {},
  26. onReady() {},
  27. methods: {
  28. // 选择地区
  29. regionChange(res) {
  30. console.log(res)
  31. this.address = res[0].name + res[1].name + res[2].name;
  32. },
  33. }
  34. }
  35. </script>

省市区json数据,请返回顶部下载。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/571123
推荐阅读
相关标签
  

闽ICP备14008679号