• 选择地址移动端弹窗 address-picker


    <template>
      <view>
        <view class="flex-row align-center" @click="set_picker()">
          <view class="flex-grow">{{ value }}</view>
          <view class="align-center">
            <u-icon color="#999797" size="14" name="arrow-right"></u-icon>
          </view>
        </view>
        <u-picker
          :closeOnClickOverlay="true"
          :show="picker_show"
          :immediateChange="true"
          :defaultIndex="picker_index"
          ref="uPicker"
          title="所在地区"
          :columns="columns"
          keyName="regionName"
          @confirm="confirm"
          @change="changeHandler"
          @close="picker_show = false"
          @cancel="picker_show = false"
        >
        </u-picker>
      </view>
    </template>
    
    <script>
    import Area from "../../common/area.js";
    export default {
      data() {
        return {
          picker_show: false,
          columns: [[], [], []],
          picker_value: "",
          picker_index: [],
          pramas: {
            province: "",
            city: "",
            area: "",
            province_dictText: "",
            city_dictText: "",
            area_dictText: "",
            picker_value: "",
            picker_idx: [],
          },
    
          province: [],
          city: [],
          county:[],
          //   value: ""
        };
      },
      props: {
        apiData: {
          type: [String, Object],
          default: "",
        },
        block: {
          type: Boolean,
          default: true,
        },
        info: {
          type: Boolean,
          default: false,
        },
        value: {
          type: String,
          default: "",
        },
        address_data: {
          type: [String, Object],
          default: () => {},
        },
        edit_btn: {
          type: Boolean,
          default: false,
        },
      },
      methods: {
        async set_picker() {
          var idx = this.picker_index;
          this.picker_show = true;
          console.log(idx);
          const picker = this.$refs.uPicker;
          console.log("picker", picker);
          if (idx.length == 0) {
            picker.setColumnValues(0, this.province);
             await this.get_city_list(this.province[0]);
            picker.setColumnValues(1, this.city);
            await this.get_county_list(this.city[0])
            picker.setColumnValues(2, this.county);
          } 
          // else {
            // picker.setColumnValues(0, this.province);
            // picker.setColumnValues(1, this.columns[0][idx[0]].children);
            // picker.setColumnValues(
            //   2,
            //   this.columns[0][idx[0]].children[idx[1]].children
            // );
    
            // picker.setIndexs(idx);
          // }
          this.$emit("on-show", this.picker_show);
        },
        confirm(e) {
          //选择城市
          //console.log('confirm', e)
          //保存选择的值
          this.pramas.province = e.value[0].regionCode;
          this.pramas.city = e.value[1].regionCode;
          this.pramas.area = e.value[2].regionCode;
          this.pramas.province_dictText = e.value[0].regionName;
          this.pramas.city_dictText = e.value[1].regionName;
          this.pramas.area_dictText = e.value[2].regionName;
          this.pramas.picker_idx = e.indexs;
          this.picker_index = e.indexs;
          this.pramas.picker_value =
            this.pramas.province_dictText +
            this.pramas.city_dictText +
            this.pramas.area_dictText;
          this.picker_show = false;
          this.$emit("on-confirm", this.pramas);
        },
        async changeHandler(e) {
          const {
            columnIndex,
            value,
            values, // values为当前变化列的数组内容
            index,
            indexs, //多级引索
            // 微信小程序无法将picker实例传出来,只能通过ref操作
            picker = this.$refs.uPicker,
          } = e;
          // 当第一列值发生变化时,变化第二列(后一列)对应的选项
          if (columnIndex === 0) {
            // picker为选择器this实例,变化第二列对应的选项
            console.log("第1联级", e);
            await this.get_city_list(this.province[e.indexs[0]]);
            picker.setColumnValues(1, this.city);
            
            //设置第3列值为引索0的默认值
            // console.log(this.city[0])
            this.get_county_list(this.city[0])
            picker.setColumnValues(2, this.county);
            //console.log(e)
          }
          if (columnIndex === 1) {
            //console.log(e)
            console.log("第2联级", e);
            // console.log("this.city",this.city)
            await this.get_county_list(this.city[e.indexs[1]])
            picker.setColumnValues(2,this.county);
          }
          this.$emit("on-change");
        },
        // 获取省
        get_province_list() {
          // 处理列表 省市县三级 数据
          let province_list = []; // 省市县
          for (let x in Area.province_list) {
            let item = {
              regionCode: x,
              regionName: Area.province_list[x],
              level: 1,
              children: [],
            };
            province_list.push(item);
          }
          this.province = province_list;
        },
        // 获取市
        get_city_list(pramas) {
          console.log("get_city_list----pramas->", pramas);
          let city_list = [];
          for (let y in Area.city_list) {
            if (y.substr(0, 2) == pramas.regionCode.substr(0, 2)) {
              let item = {
                regionCode: y,
                regionName: Area.city_list[y],
                level: 2,
                children: [],
              };
              city_list.push(item);
            }
          }
          this.city = city_list;
        },
        // 获取县/区
        get_county_list(pramas) {
          console.log("get_county_list----pramas->", pramas);
          let county_list = [];
          for (let z in Area.county_list) {
            if (z.substr(0, 4) == pramas.regionCode.substr(0, 4)) {
              let item = {
                regionCode: z,
                regionName: Area.county_list[z],
                level: 3,
                children: [],
              };
              county_list.push(item);
            }
          }
          this.county = county_list;
        },
    
        // map_indexs(data) {
        //   //遍历获取indexs
        //   if (this.edit_btn) {
        //     var search;
        //     if (this.picker_index.length == 0) {
        //       search = this.pramas.province;
        //     }
        //     if (this.picker_index.length == 1) {
        //       search = this.pramas.city;
        //     }
        //     if (this.picker_index.length == 2) {
        //       search = this.pramas.area;
        //     }
        //     if (this.picker_index.length < 3) {
        //       console.log("准备遍历", new Date().getTime());
        //       for (let i = 0; i < data.length; i++) {
        //         if (Number(search) == Number(data[i].regionCode)) {
        //           console.log("已找出", i);
        //           console.log("结束遍历", new Date().getTime());
        //           this.picker_index.push(i);
        //           console.log("picker_index", this.picker_index);
        //           //   this.map_indexs(data[i].children);
        //           break;
        //         } else {
        //           console.log("未找出", i);
        //         }
        //       }
        //     }
        //   }
        // },
      },
      mounted() {
        this.get_province_list();
      },
    };
    </script>
    
    <style scoped>
    .info {
      color: #c4c4d6;
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
  • 相关阅读:
    MODNet:基于目标分解的实时trimap-free肖像抠图
    Python:实现求一个数的因子算法(附完整源码)
    第十二章: 项目采购管理
    算法 全排列问题-(递归回溯)
    【OpenHarmony-NDK技术】简单将cJson移植到OpenHarmony中,并在c层修改参数值再返回json
    卡片介绍、EMV卡组织、金融认证---安全行业基础篇2
    AI智能文案写作工具,迅速生成高质量的文案
    vue 功能:点击增加一项,点击减少一项
    博客园美化教程
    【Java】21天学习挑战赛
  • 原文地址:https://blog.csdn.net/wangyanxin928/article/details/126366472