小程式的三級聯動

来源:https://www.cnblogs.com/rolandlee/archive/2019/04/23/10756542.html
-Advertisement-
Play Games

項目中經常遇到要選擇城市。用到三級聯動的方式 微信小程式的 組件 是三級聯動的,但是無法自定義,這讓我們心痛不已,值得我們欣慰的 picker view 組件是可以自定義添加多個選項,但還是無法聯動。既然這樣那就自己寫一個聯動。 做到如下圖所示: 分為動態獲取地址 引用靜態文件獲取地址 <! mor ...


項目中經常遇到要選擇城市。用到三級聯動的方式

  • 微信小程式的 picker 組件 mode=date 是三級聯動的,但是無法自定義,這讓我們心痛不已,值得我們欣慰的 picker-view 組件是可以自定義添加多個選項,但還是無法聯動。既然這樣那就自己寫一個聯動。
  • 做到如下圖所示:
  • 分為動態獲取地址
  • 引用靜態文件獲取地址

  • addressAdd.wxml
<view class="add-address">
  <view class="add-form">
    <view class="form-item">
      <input class="input" bindinput="bindinputName" placeholder="姓名" value="{{address.name}}" />
    </view>
    <view class="form-item">
      <input class="input" bindinput="bindinputMobile" value="{{address.mobile}}" placeholder="手機號碼" />
    </view>
    <view class="form-item">
      <input class="input" bindinput="bindinputAddress" value="{{address.address}}" placeholder="詳細地址" />
    </view>
    <view class="form-item"  bindtap='select'>
      <view class="weui-cell__bd">
        {{areaInfo}}
      </view>
    </view>

    <view class="form-default">
      <text bindtap="bindIsDefault" class="default-input {{address.isDefault == 1 ? 'selected' : ''}}">設為預設地址</text>
    </view>
  </view>

  <view class="btns">
    <button class="cannel" bindtap="cancelAddress">取消</button>
    <button class="save" bindtap="saveAddress">保存</button>
  </view>
</view>
<view class="bg-mask" bindtap="cancelSelectRegion" wx:if="{{openSelectRegion}}"></view>



<view class="picker-view" animation="{{animationAddressMenu}}" style="visibility:{{addressMenuIsShow ? 'visible':'hidden'}}">
  <!-- 確認取消按鈕 -->
  <view class='btn'>
    <text catchtap="cityCancel">取消</text>
    <text style="float: right" catchtap="citySure">確定</text>
  </view>
  <!-- 選擇地址 -->
  <picker-view class='cont' bindchange="cityChange" value="{{value}}" wx:key="">
    <!-- 省 -->
    <picker-view-column>
      <view wx:for="{{provinces}}" class="picker-item" wx:key="{{index}}">{{item.area}}</view>
    </picker-view-column>
    <!-- 市 -->
    <picker-view-column>
      <view wx:for="{{citys}}" class="picker-item" wx:key="index">{{item.area}}</view>
    </picker-view-column>
    <!-- 區 -->
    <picker-view-column>
      <view wx:for="{{areas}}" class="picker-item" wx:key="index">{{item.area}}</view>
    </picker-view-column>
  </picker-view>
</view>
  • addressAdd.wxss
page{
    height: 100%;
    background: #f4f4f4;
}
.add-address .add-form{
    background: #fff;
    width: 100%;
    height: auto;
    overflow: hidden;
}

.add-address .form-item{
    height: 116rpx;
    padding-left: 31.25rpx;
    border-bottom: 1px solid #d9d9d9;
    display: flex;
    align-items: center;
    padding-right: 31.25rpx;
}

.add-address .input{
    flex: 1;
    height: 44rpx;
    line-height: 44rpx;
    overflow: hidden;
}

.add-address .form-default{
    border-bottom: 1px solid #d9d9d9;
    height: 96rpx;
    background: #fafafa;
    padding-top: 28rpx;
    font-size: 28rpx;
}

.default-input{
    margin: 0 auto;
    display: block;
    width: 240rpx;
    height: 40rpx;
    padding-left: 50rpx;
    line-height: 40rpx;
    background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/checkbox-sed825af9d3-a6b8540d42.png) 1rpx -448rpx no-repeat;
    background-size: 38rpx 486rpx;
    font-size: 28rpx;
}

.default-input.selected{
    background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/checkbox-sed825af9d3-a6b8540d42.png) 0 -192rpx no-repeat;
    background-size: 38rpx 486rpx;
}

.add-address .btns{
    position: fixed;
    bottom: 0;
    left: 0;
    overflow: hidden;
    display: flex;
    height: 100rpx;
    width: 100%;
}

.add-address .cannel,.add-address .save{
    flex: 1;
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    font-size: 28rpx;
    color: #fff;
    border:none;
    border-radius: 0;
}

.add-address .cannel{
    background: #3F3F3F;
}

.add-address .save{
    background: #a78845;
}


.region-select{
  width: 100%;
  height: 600rpx;
  background: #fff;
  position: fixed;
  z-index: 10;
  left:0;
  bottom: 0;
}

.region-select .hd{
  height: 108rpx;
  width: 100%;
  border-bottom: 1px solid #f4f4f4;
  padding: 46rpx 30rpx 0 30rpx;
}

.region-select .region-selected{
  float: left;
  height: 60rpx;
  display: flex;
}

.region-select .region-selected .item{
  max-width: 140rpx;
  margin-right: 30rpx;
  text-align: left;
  line-height: 60rpx;
  height: 100%;
  color: #333;
  font-size: 28rpx;
  overflow: hidden;
      text-overflow: ellipsis;
    white-space: nowrap;
}

.region-select .region-selected .item.disabled{
  color: #999;
}

.region-select .region-selected .item.selected{
  color: #a78845;
}

.region-select .done{
  float: right;
  height: 60rpx;
  width: 60rpx;
  border: none;
  background: #fff;
  line-height: 60rpx;
  text-align: center;
  color: #333;
  font-size: 28rpx;
}

.region-select .done.disabled{
  color: #999;
}

.region-select .bd{
  height: 492rpx;
  width: 100%;
  padding: 0 30rpx;
}

.region-select .region-list{
  height: 492rpx;
}

.region-select .region-list .item{
  width: 100%;
  height: 104rpx;
  line-height: 104rpx;
  text-align: left;
  color: #333;
  font-size: 28rpx;
}

.region-select .region-list .item.selected{
  color: #b4282d;
}


.bg-mask{
  height: 100%;
  width: 100%;
  background: rgba(0, 0, 0, 0.4);
  position: fixed;
  top:0;
  left:0;
  z-index: 8;
}




.picker-view {
    width: 100%;
    display: flex;
    z-index:12;
    background-color: #fff;
    /* background: rgba(0, 0, 0, .2); */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: fixed;
    bottom: 0;
    left: 0rpx;
    height: 40vh;
}
.btn {
    width: 100%;
    height: 90rpx;
    padding: 0 24rpx;
    box-sizing: border-box;
    line-height: 90rpx;
    text-align: center;
    display: flex;
    background: rgba(255,255,255,.8);
    justify-content: space-between;
}
.cont {
    width: 100%;
    height: 389rpx;
}
.picker-item {
    line-height: 70rpx;
    margin-left: 5rpx;
    margin-right: 5rpx;
    text-align: center;
}
.address {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    text-align: center;
    border-bottom: 1rpx solid #f1f1f1;
}
  • addressAdd.js (分兩個版本一個是動態獲取的 就是選擇的時候動態向後臺獲取內容 下方是動態獲取的例子:)
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();
Page({
  data: {
    addressId: 0,
    openSelectRegion: false,
    regionType: 1,
    selectRegionDone: false,
    szxqList: [],
    szxq: {
      id: "",
      name: "請選擇小區"
    },
    szdsList: [],
    szds: {
      id: "",
      name: ""
    },
    fanghao: "",
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo: '',
    areaJson: {}
  },
  bindinputMobile(event) {
    let address = this.data.address;
    address.mobile = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputName(event) {
    let address = this.data.address;
    address.name = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputAddress(event) {
    let address = this.data.address;
    address.address = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindIsDefault() {
    let address = this.data.address;
    address.isDefault = !address.isDefault;
    this.setData({
      address: address
    });
  },
  getAddressDetail() {
    let that = this;
    // util.request(api.AddressDetail, {
    //   id: that.data.addressId
    // }).then(function(res) {
    //   if (res.errno === 0) {
    //     if (res.data) {
    //       that.setData({
    //         address: res.data
    //       });
    //     }
    //   }
    // });
  },

  wxChooseAddress() {
    let that = this;
    let address = this.data.address;
    // 用戶已經同意小程式使用地址功能
    wx.chooseAddress({
      success: function(res) {
        address.provinceId = 99999;
        address.cityId = 88888;
        address.areaId = 77777;
        address.name = res.userName;
        address.mobile = res.telNumber;
        address.provinceName = res.provinceName;
        address.cityName = res.cityName;
        address.areaName = res.countyName;
        address.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;

        that.setData({
          address: address,
        });
      }
    });
  },

  wxAddress() {
    let that = this;
    // 可以通過 wx.getSetting 先查詢一下用戶是否授權了 "scope.address" 這個 scope
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.authorize({
            scope: 'scope.address',
            success() {
              that.wxChooseAddress();
            }
          })
        } else {
          that.wxChooseAddress();
        }
      }
    })
  },

  onLoad: function(options) {
    let that = this;
    // 頁面初始化 options為頁面跳轉所帶來的參數
    console.log(options);
    if (options.id && options.id != 0) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    } else {
      that.wxAddress();
    }
  },


  onReady: function() {

  },

  cancelAddress() {
    wx.navigateBack();
  },
  saveAddress() {
    console.log(this.data.address);
    let address = this.data.address;

    if (address.name == '') {
      util.showErrorToast('請輸入姓名');

      return false;
    }

    if (address.mobile == '') {
      util.showErrorToast('請輸入手機號碼');
      return false;
    }


    if (address.areaId == 0) {
      util.showErrorToast('請輸入省市區');
      return false;
    }

    if (address.address == '') {
      util.showErrorToast('請輸入詳細地址');
      return false;
    }

    let that = this;

  },
  onShow: function() {

    // 獲取所在棟數
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear',
    })
    this.animation = animation

    const that = this

    // 獲取所在地區
    console.log()
    util.getAreaReq().then(provinces => {
      util.getAreaReq(provinces[0].code).then(citys => {
        util.getAreaReq(citys[0].code).then(areas => {

          that.setData({
            provinces: provinces,
            citys: citys,
            areas: areas,
            areaJson: {
              provinces: {
                id: 40,
                name: "廣東省"
              },
              citys: {
                id: 4006,
                name: "河源市"
              },
              areas: {
                id: 400602,
                name: "源城區"
              }
            }
          })


          var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
          var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas


          that.setData({
            areaInfo: areaInfo,
          })
        })
      })
    })
   
  },

  // 點擊所在地區彈出選擇框
  select: function(e) {
    // 如果已經顯示,不在執行顯示動畫
    if (this.data.addressMenuIsShow) {
      return false
    } else {
      // 執行顯示動畫
      this.startAddressAnimation(true)
    }
  },

  // 處理省市縣聯動邏輯
  cityChange: function(e) {
    // console.log(this.data.provinces)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    var that = this;
    // console.log(provinces)
    // 如果省份選擇項和之前不一樣,表示滑動了省份,此時市預設是省的第一組數據,
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      // console.log(citys[cityNum])

      util.getAreaReq(provinces[provinceNum].code).then(citys => {
          util.getAreaReq(citys[0].code).then(areas => {

            this.setData({
              value: [provinceNum, 0, 0],
              citys: citys,
              areas: areas,
              areaJson: {
                provinces: {
                  id: provinces[provinceNum].code,
                  name: provinces[provinceNum].area
                },
                citys: {
                  id: citys[0].code,
                  name: citys[0].area
                },
                areas: {
                  id: areas.length > 0 ? areas[0].code : null,
                  name: areas.length > 0 ? areas[0].area : null,
                }
              }
            })
          })
        })


    } else if (this.data.value[1] != cityNum) {
      // 滑動選擇了第二項數據,即市,此時區顯示省市對應的第一組數據
      var id = citys[cityNum].id

      util.getAreaReq(citys[cityNum].code).then(areas => {
          this.setData({
            value: [provinceNum, cityNum, 0],
            areas: areas,
            areaJson: {
              provinces: {
                id: provinces[provinceNum].code,
                name: provinces[provinceNum].area
              },
              citys: {
                id: citys[cityNum].code,
                name: citys[cityNum].area
              },
              areas: {
                id: areas.length > 0 ? areas[0].code : null,
                name: areas.length > 0 ? areas[0].area : null,
              }
            }
          })
       
        })

    } else {
      // 滑動選擇了區

      this.setData({
        value: [provinceNum, cityNum, countyNum],
        areaJson: {
          provinces: {
            id: provinces[provinceNum].code,
            name: provinces[provinceNum].area
          },
          citys: {
            id: citys[cityNum].code,
            name: citys[cityNum].area
          },
          areas: {
            id: areas[countyNum].code,
            name: areas[countyNum].area
          }
        }
      })
      // console.log(that.data.areaJson)
    }
  },

  // 執行動畫
  startAddressAnimation: function(isShow) {
    if (isShow) {
      // vh是用來表示尺寸的單位,高度全屏是100vh
      this.animation.translateY(0 + 'vh').step()
    } else {
      this.animation.translateY(40 + 'vh').step()
    }
    this.setData({
      animationAddressMenu: this.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 點擊地區選擇取消按鈕
  cityCancel: function(e) {
    this.startAddressAnimation(false)
  },
  // 點擊地區選擇確定按鈕
  citySure: function(e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    this.startAddressAnimation(false)
    // console.log(that.data.areaJson)
    var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
    // 將選擇的城市信息顯示到輸入框
    var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas


    that.setData({
      areaInfo: areaInfo,
    })
  },
  onHide: function() {
    // 頁面隱藏

  },
  onUnload: function() {
    // 頁面關閉

  }
});
  • 需要使用外部js(utils) 自己封裝的一個工具
var api = require('../config/api.js');
var app = getApp();
var user = require('./user.js');

/**
 * 封裝微信的的request
 */
function request(url, data = {}, method = "GET") {
  return new Promise(function(resolve, reject) {

    user.checkLogin().then(res => {

    }).catch(() => {
      wx.switchTab({
        url: '/pages/ucenter/index/index?show=true'
      });
    });

    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie': "token=" + wx.getStorageSync('token') + ";" + wx.getStorageSync('sessionid'),
        'X-Requested-With': "XMLHttpRequest"
      },
      success: function(res) {
        if (res.statusCode == 400) {
          user.loginByWeixin().then(res => {
            app.globalData.hasLogin = true;

          });
          wx.redirectTo({
            url: '/pages/index/index'
          });
          wx.showToast({
            title: '已經重新登錄',
          })
        }
        if (res.header["Set-Cookie"]) {
          wx.setStorageSync("sessionid", res.header["Set-Cookie"])
        }
        if (res.statusCode == 200) {

          if (res.data.errno == 501) {

          } else {
            resolve(res);
          }
        } else {

          reject(res);
        }

      },
      fail: function(err) {
        reject(err)
      }
    })
  });
}



function getAreaReq(id) {
  const that = this;
  return new Promise(function(resolve, reject) {
    that.request("****", JSON.stringify({
 
    }), "post").then(response => {
      console.log(response.data.rs_data)

      resolve(response.data.rs_data);

    })
  })
}


module.exports = {
  request,
  getAreaReq 
};
  • 使用靜態獲取的時候。js如下
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var area = require('../../../config/area.js');

var app = getApp();
Page({
  data: {
    address: {
      id: 0,
      provinceId: 0,
      cityId: 0,
      areaId: 0,
      address: '',
      name: '',
      mobile: '',
      isDefault: 0,
      provinceName: '',
      cityName: '',
      areaName: ''
    },
    addressId: 0,
    openSelectRegion: false,
    regionType: 1,
    selectRegionDone: false,
    szxqList: [],
    szxq: {
      id: "",
      name: "請選擇小區"
    },
    szdsList: [],
    szds: {
      id: "",
      name: ""
    },
    fanghao: "",
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo: '',
    areaJson: {}
  },
  bindinputMobile(event) {
    let address = this.data.address;
    address.mobile = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputName(event) {
    let address = this.data.address;
    address.name = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputAddress(event) {
    let address = this.data.address;
    address.address = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindIsDefault() {
    let address = this.data.address;
    address.isDefault = !address.isDefault;
    this.setData({
      address: address
    });
  },
  getAddressDetail() {
    let that = this;
    util.request(api.AddressDetail, {
      id: that.data.addressId
    }).then(function(res) {
      if (res.errno === 0) {
        if (res.data) {
          that.setData({
            address: res.data
          });
        }
      }
    });
  },

  wxChooseAddress() {
    let that = this;
    let address = this.data.address;
    // 用戶已經同意小程式使用地址功能
    wx.chooseAddress({
      success: function(res) {
        address.provinceId = 99999;
        address.cityId = 88888;
        address.areaId = 77777;
        address.name = res.userName;
        address.mobile = res.telNumber;
        address.provinceName = res.provinceName;
        address.cityName = res.cityName;
        address.areaName = res.countyName;
        address.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;

        that.setData({
          address: address,
        });
      }
    });
  },

  wxAddress() {
    let that = this;
    // 可以通過 wx.getSetting 先查詢一下用戶是否授權了 "scope.address" 這個 scope
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.authorize({
            scope: 'scope.address',
            success() {
              that.wxChooseAddress();
            }
          })
        } else {
          that.wxChooseAddress();
        }
      }
    })
  },

  onLoad: function(options) {
    let that = this;
    // 頁面初始化 options為頁面跳轉所帶來的參數
    console.log(options);
    if (options.id && options.id != 0) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    } else {
      that.wxAddress();
    }
  },


  onReady: function() {

  },

  cancelAddress() {
    wx.navigateBack();
  },
  saveAddress() {
    console.log(this.data.address);
    let address = this.data.address;

    if (address.name == '') {
      util.showErrorToast('請輸入姓名');

      return false;
    }

    if (address.mobile == '') {
      util.showErrorToast('請輸入手機號碼');
      return false;
    }


    if (address.areaId == 0) {
      util.showErrorToast('請輸入省市區');
      return false;
    }

    if (address.address == '') {
      util.showErrorToast('請輸入詳細地址');
      return false;
    }

    if (!check.isValidPhone(address.mobile)) {
      util.showErrorToast('手機號不正確');
      return false;
    }
  },
  onShow: function() {

    // 獲取所在棟數
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear',
    })
    this.animation = animation



    util.request("https://www.xaibox.com/czbb/interface/dataInfo.php", JSON.stringify({
      "param_key": {
        "info_mode": "getcity_jd"
      },
      "secret_key": "047709aaa7df22205d818bf4c1707458"
    }), "post").then(response => {
      console.log(response)

      that.setData({
        szxqList: response.data.rs_data
      })
      that.setData({
        szxq: response.data.data[0]
      })
      that.setData({
        szds: response.data.data[0]['buildingList']['0']
      })
      that.setData({
        szdsList: response.data.data[0]['buildingList']
      })

    })
    // 獲取所在地區



    that.setData({
      provinces: areajs,
      citys: areajs[0].children,
      areas: areajs[0].children ? areajs[0].children[0].children : [],
      areaJson: {
        provinces: {
          id: 40,
          name: "廣東省"
        },
        citys: {
          id: 4006,
          name: "河源市"
        },
        areas: {
          id: 400602,
          name: "源城區"
        }
      }
    })


    var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
    var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas


    that.setData({
      areaInfo: areaInfo,
    })
  },

  // 點擊所在地區彈出選擇框
  select: function(e) {
    // 如果已經顯示,不在執行顯示動畫
    if (this.data.addressMenuIsShow) {
      return false
    } else {
      // 執行顯示動畫
      this.startAddressAnimation(true)
    }
  },

  // 處理省市縣聯動邏輯
  cityChange: function(e) {
    // console.log(this.data.provinces)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    var that = this;
    // console.log(provinces)
    // 如果省份選擇項和之前不一樣,表示滑動了省份,此時市預設是省的第一組數據,
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      // console.log(citys[cityNum])
      this.setData({
        value: [provinceNum, 0, 0],
        citys: provinces[provinceNum].children,
        areas: provinces[provinceNum].children ? provinces[provinceNum].children[0].children : [],
        areaJson: {
          provinces: {
            id: provinces[provinceNum].code,
            name: provinces[provinceNum].area
          },
          citys: {
            id: provinces[provinceNum].children[0].code,
            name: provinces[provinceNum].children[0].area
          },
          areas: {
            id: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].code : null,
            name: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].area : null
          }
        }
      })


    } else if (this.data.value[1] != cityNum) {
      // 滑動選擇了第二項數據,即市,此時區顯示省市對應的第一組數據
      var id = citys[cityNum].id

      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: citys[cityNum].children,
        areaJson: {
          provinces: {
            id: provinces[provinceNum].code,
            name: provinces[provinceNum].area
          },
          citys: {
            id: citys[cityNum].code,
            name: citys[cityNum].area
          },
          areas: {
            id: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].code : null,
            name: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].area : null
          }
        }
      })
    } else {
      // 滑動選擇了區

      this.setData({
        value: [provinceNum, cityNum, countyNum],
        areaJson: {
          provinces: {
            id: provinces[provinceNum].code,
            name: provinces[provinceNum].area
          },
          citys: {
            id: citys[cityNum].code,
            name: citys[cityNum].area
          },
          areas: {
            id: areas[countyNum].code,
            name: areas[countyNum].area
          }
        }
      })
      // console.log(that.data.areaJson)
    }
  },

  // 執行動畫
  startAddressAnimation: function(isShow) {
    if (isShow) {
      // vh是用來表示尺寸的單位,高度全屏是100vh
      this.animation.translateY(0 + 'vh').step()
    } else {
      this.animation.translateY(40 + 'vh').step()
    }
    this.setData({
      animationAddressMenu: this.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 點擊地區選擇取消按鈕
  cityCancel: function(e) {
    this.startAddressAnimation(false)
  },
  // 點擊地區選擇確定按鈕
  citySure: function(e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    this.startAddressAnimation(false)
    // console.log(that.data.areaJson)
    var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
    // 將選擇的城市信息顯示到輸入框
    var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas

    that.setData({
      areaInfo: areaInfo,
    })
  },
  onHide: function() {
    // 頁面隱藏

  },
  onUnload: function() {
    // 頁面關閉

  }
});
  • 靜態獲取三級聯動 的話則需要文件area.js點擊下載

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 利用 vue-cli 搭建的項目大都是單頁面應用項目,對於簡單的項目,單頁面就能滿足要求。但對於有多個子項目的應用,如果創建多個單頁面,顯示有點重覆,特別是 node_modules 會有多份相同的。如果全部放到單頁面項目下,又顯得有點亂,這時候通過改造 vue-cli 搭建的項目為多頁面,就是一個 ...
  • <!doctype html> <html> <head> <meta charset="urtf-8"> <title>通過js改變class名字改變樣式</title> <style> .tao { /*初始樣式*/ border-radius:35px; width:100px; height ...
  • 1.jQuery對象 jQuery包裝集對象 獲得mydiv的Jquery對象: var divJquery = $("#mydiv"); 2.基礎選擇器 1、ID選擇器格式:$("#id屬性值") 獲取指定id值的對象(只會獲取到第一個id的值)2、類選擇器格式:$(".class屬性值")獲取所 ...
  • 註: 依賴第三方插件 clipboard 一、安裝插件 二、全局註入(main.js) 三、使用 四、效果圖 ...
  • 最近公司有需求要做一些可視化的功能。之前一直都是用Echarts做的,但是Echarts現在滿足不了我們的需求,經過多方請教,查找發現只有D3可以滿足我們的需求,而且可以完美的適用到react框架中。第一次接觸到D3,發現這些圖標的可交互性非常豐富,而且動畫流暢簡潔。 所以,打算學習D3並且應用到項 ...
  • 今天練習一個小demo, 從本地讀取圖片, 然後實現類似淘寶放大鏡的效果, 再加兩個需求 1 .可以調節縮放比例,預設放大兩倍 2 . 圖片寬高自適應, 不固定寬高 話不多說先看效果: 原理:1, 右側放大區域的大小等於左側半透明滑塊大小乘以縮放倍數 2, 右側放大區域圖片的原始尺寸要和 左側圖片一 ...
  • <html><body> <h3>js控制文件上傳數量</h3> <form action="" enctype="multipart/form-data"> <input type="file" name="file" multiple="multiple" onchange="fileCount ...
  • 一、什麼是小程式? 基於微信的可以為用戶提供一些服務的web項目,利用微信提供的介面可以讓所有開發者使用到微信的原生能力,去完成一些之前做不到或者難以做到的事情。 二、小程式開發工具以及語言? 小程式需要用到微信提供的小程式開發工具,​小程式的主要開發語言是 JavaScript 。 三、小程式與普 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...