小程序 后台动态数据 省市二级联动

<picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange"  value="{{multiIndex}}" range="{{multiArray}}">
  <view class="picker">
      当前选择:{{multiArray[0][multiIndex[0]]}} > {{multiArray[1][multiIndex[1]]}}
    </view>
</picker>
Page({
  data: {
    multiIndex: []
  },
//市级
  searchClassInfo(xiaoqu_id) {
    var that = this;
    if (xiaoqu_id) {
      this.setData({
        pid: xiaoqu_id
      }, () => {
        wx.request({
          url: 'http://192.168.3.137:81/api/v2/getCityListByProvinceId',
          https: '',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          data: {
            "province_id": that.data.pid
          },
          success: function(res) {
            console.log(res);
            var classList = res.data;
            var classArr = classList.map(item => {
              console.log(item.name)
              return item.name;
            })
            var xiaoquArr = that.data.xiaoquArr;
            that.setData({
              multiArray: [xiaoquArr, classArr],
              classArr,
              classList
            })
          }
        })
      })
    }
  },

  bindMultiPickerColumnChange: function(e) {
    //e.detail.column 改变的数组下标列, e.detail.value 改变对应列的值
    console.log(e);
    console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };

    data.multiIndex[e.detail.column] = e.detail.value;
    var pid_session = this.data.pid;     // 保持之前的省id 与新选择的id 做对比,如果改变则重新请求数据
    switch (e.detail.column) {
      case 0:
      var xiaoquList = this.data.xiaoquList;
        var pid = xiaoquList[e.detail.value]['id'];
        if (pid_session != pid) {     // 与之前保持的省id做对比,如果不一致则重新请求并赋新值
          this.searchClassInfo(pid);
        }
        data.multiIndex[1] = 0;
        break;
    }
    this.setData(data);
  },

  bindMultiPickerChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    var class_key = 0;
    var classList = this.data.classList;
    var select_key = e.detail.value[1];
    var real_key = select_key - 1;
    if (real_key < class_key) {
      this.setData({
        cid: 0
      })
    } else {
      this.setData({
        cid: classList[real_key]['cid']       // class_id 代表着选择的市级对应的 班级id
      })
    }
    this.setData({
      multiIndex: e.detail.value
    })
  },
//省级
  onLoad: function() {
    var that = this;
    wx.request({
      url: 'http://192.168.3.137:81/api/v2/getCityListByProvinceId',
      data: {
        province_id:1
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log(res);
        var xiaoquList = res.data;
        console.log(xiaoquList)
        var xiaoquArr = xiaoquList.map(item => {     // 此方法将省名称区分到一个新数组中
          console.log(item.name)
          return item.name;
        });
        console.log(xiaoquArr)
        that.setData({
          multiArray: [xiaoquArr, []],
          xiaoquList,
          xiaoquArr
        })
        var default_xiaoqu_id = xiaoquList[0]['id'];     //获取默认的省对应的id
        if (default_xiaoqu_id) {
          that.searchClassInfo(default_xiaoqu_id)       // 如果存在调用获取对应的省级数据
        }
      }
    })
  }
})

 

转载于:https://www.cnblogs.com/wangshishuai/p/10497369.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值