小程序下拉框

页面

 <view class="info info-name">
				<label class="info-lable">所属街道</label>
		
				<picker bindchange="bindPickerChange2" range-key='{{"cboText"}}' style="float:left; margin-left: 5%" range="{{AreaCode}}">
					<view>
						<text style="color:#848484">{{AreaCode[qx].cboText}}</text>
					</view>
				</picker>
			</view>

JS

data{
  AreaCode: [{
      cboValue: '',
      cboText: '请选择区县'
    }],
    selectQA: '',
    qx:0
    }

onload:function(options)
{
this.GetAreaCode()//调用查询方法
//回填下拉框数值
查询成功后再setData中赋值
this.setData({
selectQA:.areaCode,
})
}
  GetAreaCode: function () {

    var that = this;
    wx.request({
      url: app.globalData.ApiPath + 'api/Common/GetComAreaCodeList',
      method: 'post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {

        that.setData({
          ['AreaCode[0].cboValue']: '',
          ['AreaCode[0].cboText']: '请选择区县'
        })

        for (var i = 0; i < res.data.length; i++) {
       
          var cboValue = 'AreaCode[' + (i + 1) + '].cboValue';
          var cboText = 'AreaCode[' + (i + 1) + '].cboText';
          //console.log(cboValue,cboText)
          that.setData({
            [cboValue]: res.data[i].areaCode,
            [cboText]: res.data[i].areaName
          })
        }
        if (that.data.selectQA != "") {
          for (let i = 0; i < that.data.AreaCode.length; i++) {
            if (that.data.AreaCode[i].cboValue == that.data.selectQA) {
              that.setData({
                qx: i
              })
            }
          }
        }
        //console.log(that.data.AreaCode)
      }
    })
  },
  //获取下拉框值
   bindPickerChange2: function (e) {

    //console.log('picker发送选择改变,携带值为', e.detail.value)
    var areacode = this.data.AreaCode[e.detail.value].cboValue;
    this.setData({
      qx: e.detail.value,
      selectQA: areacode
    })
   
  },
上一篇:微信小程序 - setdata 修改数组和对象


下一篇:泛型接口