1. 程式人生 > >微信小程序之地址聯動

微信小程序之地址聯動

ndt share 收件人 body otto 選擇 add har jpg

技術分享圖片

這就是我們要實現的效果

<view class="consignee"> 
<!-- consignee 收件人 -->
  <text>收件人: </text><input type=‘text‘ placeholder=‘請輸入姓名‘></input>
</view>
<!-- 手機號 -->
<view class="consignee"> 

  <text>手機號: </text><input type=‘text‘ placeholder=‘請輸入手機號‘
></input> </view> <!--所在地區 --> <view bindtap=‘selectDistrict‘ class=‘consignee‘> <text >所在地區</text> <text>{{areaInfo}}</text> </view> <!-- 詳細地址 --> <view class=‘consignee‘> <text>詳細地址</text> <input type=‘text‘ placeholder
=‘請輸入詳細地址‘></input> </view> <!-- 完成 --> <button class=‘btn‘>完成</button> <view class="picker-view" animation="{{animationAddressMenu}}" style="visibility:{{addressMenuIsShow ? ‘visible‘:‘hidden‘}}"> <view style="height:10% ;width:95%;margin-top:10rpx"> <
text catchtap="cityCancel">取消</text> <text style="float: right" catchtap="citySure">確定</text> </view> <!--"可以顯示默認的城市,使用後級聯選擇城市反應很慢就不使用了--> <picker-view style="width: 100%; height: 300px;" bindchange="cityChange" value="{{value}}" wx:key=""> <picker-view-column> <view wx:for="{{provinces}}" class="picker-item" wx:key="index"> {{item.name}}</view> </picker-view-column> <picker-view-column> <view wx:for="{{citys}}" class="picker-item" wx:key=""> {{item.name}}</view> </picker-view-column> <picker-view-column> <view wx:for="{{areas}}" class="picker-item" wx:key=""> {{item.name}}</view> </picker-view-column> </picker-view> </view>

給“所在地區” 綁定點擊事件 ‘selectDestrict’

也就是通過觸發這個方法從而我們要展示什麽 在這裏也就是展示picker-view組件

<!--"可以顯示默認的城市,使用後級聯選擇城市反應很慢就不使用了-->
  <picker-view style="width: 100%; height: 300px;" bindchange="cityChange" value="{{value}}" wx:key="">
    <picker-view-column>
      <view wx:for="{{provinces}}" class="picker-item" wx:key="index">
        {{item.name}}</view>
    </picker-view-column>
    <picker-view-column>
      <view wx:for="{{citys}}" class="picker-item" wx:key="">
        {{item.name}}</view>
    </picker-view-column>
    <picker-view-column>
      <view wx:for="{{areas}}" class="picker-item" wx:key="">
        {{item.name}}</view>
    </picker-view-column>
  </picker-view>

可以看到 我們可以在picker-view標簽中定義他的寬高 以及當滑動的時候所觸發的方法‘cityChange’

其余的就是利用wx:for的方法去得到相應的數據 巴拉巴拉的 那接下來就是JS階段了

// pages/address/newAdd.js
let address = require(‘../../utils/city.js‘)
Page({

  /**
   * 頁面的初始數據
   */
  data: {
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo: ‘‘ 
  },

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
  
  },
  // 點擊所在地區彈出選擇框
  selectDistrict: function (e) {
    var that = this
    // 如果已經顯示,不在執行顯示動畫
    if (that.data.addressMenuIsShow) {
      return
    }
    // 執行顯示動畫
    that.startAddressAnimation(true)
  },
  // 執行動畫
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    // if (isShow) {
    //   // vh是用來表示尺寸的單位,高度全屏是100vh
    //   that.animation.translateY(0 + ‘vh‘).step()
    // } else {
    //   that.animation.translateY(40 + ‘vh‘).step()
    // }
    that.setData({
      //animationAddressMenu: that.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
    that.startAddressAnimation(false)
    // 將選擇的城市信息顯示到輸入框
    var areaInfo = that.data.provinces[value[0]].name + ‘,‘ + that.data.citys[value[1]].name + ‘,‘ + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
    })
  },
  // 點擊蒙版時取消組件的顯示
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  // 處理省市縣聯動邏輯
  cityChange: function (e) {
    console.log(e)
    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]
    // 如果省份選擇項和之前不一樣,表示滑動了省份,此時市默認是省的第一組數據,
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      // 滑動選擇了第二項數據,即市,此時區顯示省市對應的第一組數據
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      // 滑動選擇了區
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },
  /**
   * 生命周期函數--監聽頁面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函數--監聽頁面顯示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函數--監聽頁面隱藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函數--監聽頁面卸載
   */
  onUnload: function () {
  
  },

  /**
   * 頁面相關事件處理函數--監聽用戶下拉動作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 頁面上拉觸底事件的處理函數
   */
  onReachBottom: function () {
  
  },

  /**
   * 用戶點擊右上角分享
   */
  onShareAppMessage: function () {
  
  }
})

微信小程序之地址聯動