小程序级联选择(接口请求后展示picker

实现效果

级联选择,下级选项与上级的选择有关,且每次都要重新调用接口获取选项
在这里插入图片描述

<template>
  <view class="wrapper-region-tree">
    <view wx:for="{{regionList}}" wx:key="index" class="region-item">
       <viewstyle="white-space: nowrap">{{ item.title }}</view>
       <text style="color: red">*</text>
       <view class="choose-wrap" @tap="clickRegion({{index}})">
         <view wx:if="{{ item.value }}">{{ item.value.name }}</view>
         <view wx:else style="color: #929292">请选择</view>
         <image class="img-type-more" src="../resources/icon_right.png" />
       </view>
    </view>
    <picker-view indicator-class='pickerCol' bindchange="bindChange" class="{{ isShowPicker ? 'show' : '' }}" wx:if="{{ isShowPicker }}">
      <view class='btns'>
        <view bindtap="closePicker">取消</view>
        <view bindtap="getValue">确认</view>
      </view>
      <picker-view-column>
        <view wx:for="{{chooseColumns}}" wx:key="index" class="{{ pickerIndex === index ? 'pick-text' : '' }}">{{item.name}}</view>
      </picker-view-column>
    </picker-view>
  </view>
</template>

样式

<style scoped lang="less">
  .wrapper-region-tree {
    background: white;
    padding: 24rpx 34rpx;

    .region-item {
      display: flex;
      align-items: center;
      font-size: 32rpx;
      line-height: 80rpx;
    }
  }

  .choose-wrap {
    display: flex;
    flex-grow: 1;
    justify-content: flex-end;
    align-items: center
  }
  .img-type-more {
    margin-left: 8rpx;
    width: 16rpx;
    height: 24rpx;
  }

  // 设置头部的取消和确定按钮
  picker-view .btns {
    width: 100%;
    height: 100rpx;
    color: #3388ff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 5;
  }

  picker-view .btns view {
    width: 20%;
    text-align: center;
    font-size: 32rpx;
  }

  picker-view {
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #FFF;
    text-align: center;
    box-sizing: border-box;
    width: 100%;
    height: 0;
    border-radius: 30rpx 30rpx 0 0;
    z-index: 999;
  }

  // 添加弹出的过渡动画
  picker-view.show {
    height: 36%;
    transition: all 0.4s;
  }

  // 设置单列数据的样式
  picker-view-column {
    border-radius: 30rpx 30rpx 0 0;
    color: #B8B8B8;
    font-size: 32rpx;
    margin-top: 47rpx;
  }

  // 设置选中框的样式
  .pickerCol {
    width: 100%;
    height: 50rpx;
    color: #3388ff;
    border-top: 1px solid #f2f3f5;
    border-bottom: 1px solid #f2f3f5;
  }

  // 自定义选中的时候字体颜色和大小
  .pick-text {
    color: #3388ff;
    font-size: 34rpx;
  }
</style>
data = {
    isShowPicker: false,
    pickerIndex: 0,
    regionList: [ // 接口请求对于各级有什么需要都可以写在这里,方便取,此需求只需要下标所以没加
      {},  // 占位,保证下标index为接口传参regionLevel
      {
        title: '所属省份',
        value: null
      },
      {
        title: '所属地市',
        value: null
      },
      {
        title: '所属区县',
        value: null
      },
    ],
    selectIndex: -1, // 上一次级联选择index,避免重复请求
    chooseColumns: [],
  }
  methods = {
    clickRegion(curRegionLevel = 1) {
      if (curRegionLevel === this.selectIndex) {
      	// 不用重复请求
        this.isShowPicker = true;
        return;
      }
      let seniorID = -1; // 上级区划选择的id值
      if (this.regionLevel < curRegionLevel) {
      	// 判断上级区划是否已选择
        seniorID = this.regionList[curRegionLevel - 1].value && this.regionList[curRegionLevel - 1].value.id
        if (!seniorID && seniorID !== 0) {
          toast('请先选择上级区划!')
          return;
        }
      }
      // 后端说根据上级选择过滤则传seniorID,取此级所有选项则传regionType
      const params = this.regionLevel < curRegionLevel ? { seniorID: seniorID } : { regionType: curRegionLevel };
      Http.ajax({
        methodName: '/mi/region/getregionlist',
        params,
        start: () => {
          openLoading()
        },
        success: data => {
          if (data && data.success) {
            this.chooseColumns = (data.data || []).map(val => {
              return { // 后续需要的值
                name: val.regionName,
                id: val.regionID,
              };
            });
            this.selectIndex = curRegionLevel;
            this.isShowPicker = true;
            this.$apply()
          } else {
            toast('获取数据失败')
          }
        },
        fail: (res) => {
          toast('接口调用失败')
        },
        complete: () => {
          closeLoading()
        }
      })
    },
    bindChange(e) {
      this.pickerIndex = e.detail.value[0];
    },
    closePicker() {
      this.pickerIndex = 0;
      this.isShowPicker = false;
    },
    getValue() {
      const curItem = this.chooseColumns[this.pickerIndex];
      this.regionList.forEach((item, index) => {
        if (index === this.selectIndex) {
          item.value = curItem;
        } else if (index > this.selectIndex) {
          item.value = null; // 级联选择,需要清空下级选择
        }
      });
      this.pickerIndex = 0;
      this.isShowPicker = false;
      // 在此处保存值
    },
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值