微信小程序picker动态加载二级下拉数据

废话不多说 直接上代码,
后台返回数据为json:

<view class="title">种植产品</view>
        <picker mode="multiSelector" range="{{[provinceList, provinceList[in].children]}}" range-key="xhycategorizeName"  bindcolumnchange="bindcolumnchange" bindchange="bindchange">
            <view class="detail_default">
            {{index==-1||child_index==-1?'请选择分类':provinceList[index].xhycategorizeName+','+provinceList[index].children[child_index].xhycategorizeName}}
            </view>
        </picker>
// pages/address_add/address_add.js
const app = getApp()
var api = require('../../../api.js')
var util = require('../../../utils/util.js')
var WxParse = require('../../../wxParse/wxParse.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:-1,
    child_index:-1,
    childs_index:-1,
    in:0,
    ins:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      xhyactivityId:options.xhyactivityId,
      xhylevelId:options.xhylevelId,
      xhyprizesId:options.xhyprizesId,
      xhymembersId:options.xhymembersId,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if (wx.getStorageSync('hasCode')) {
      that.getPage().then(function (data) {
        // return  app.getSite()
      })
	  .then(function (data) {
              return that.getProvince()
            })
      
    } else {
      app.login().then(function (data) {
      })
       
		.then(function (data) {
              return that.getProvince()
            })
			
        
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  
  
  getProvince: function (params) {
    var that = this;
    var p = new Promise(function (resolve, reject) {
      wx.request({
        url: api.admin.xhycategorize,//你的后台访问的数据url
        data:{
          xhyactivityId: that.data.xhyactivityId,
          xhycategorizeParenid:-1,//后台精心过处理,根据自己的情况自己修改
          xhycategorizeType:0,
          isAvailable:1,
        },
        header: wx.getStorageSync('header'),
        method: 'POST',
        dataType: 'json',
        success: (result) => {
          resolve(result)
          console.log("result==="+result.data)
          console.log(result.data)
          var provinceArr = [];
          var provincePid = [];
          
          that.setData({
            provinceList: result.data,
          })
        },
        fail: () => {
        },
        complete: () => { }
      })
    })
    return p
  },
  bindRegionChangeP: function (params) {
    console.log(params)
    this.setData({
      province: this.data.provinceList[(params.detail.value)],
      pId: this.data.provincePidArr[(params.detail.value)],
    })
    this.getCitys()
  },
  bindcolumnchange(e){
    console.log(e.detail)
    
    if(e.detail.column==0){
      this.setData({
        in:e.detail.value
      })
    }
    if(e.detail.column==1){
      this.setData({
        ins:e.detail.value
      })
    }
  },
  bindchange(e){
    console.log('用户选择的所有分类');
    console.log(e)
    this.setData({
      index:e.detail.value[0],
      child_index:e.detail.value[1]
    })
  },
  getCitys: function (params) {
    var that = this;
    var p = new Promise(function (resolve, reject) {
      wx.request({
        url: api.admin.xhycategorize,
        data: {
          xhyactivityId: that.data.xhyactivityId,
          xhycategorizeParentid:that.data.pId,
          xhycategorizeType:0,
          isAvailable:1,
        },
        header: wx.getStorageSync('header'),
        method: 'POST',
        dataType: 'json',
        success: (result) => {
          resolve(result)
          var citysArr = [];
          var citysCid = [];
          result.data.forEach(element => {
            citysArr.push(element.xhycategorizeName),
              citysCid.push(element.xhycategorizeId)
          });
          that.setData({
            citysList: citysArr,
            citysCidArr: citysCid
          })
        },
        fail: () => {
        },
        complete: () => { }
      })
    })
    return p
  },
  bindRegionChangeC: function (params) {
    this.setData({
      citys: this.data.citysList[(params.detail.value)],
      cId: this.data.citysCidArr[(params.detail.value)],
    })
   
  },
  
  

  //获奖信息提交
  saveXhywinning: function (params) {
    var that = this;

    var p = new Promise(function (resolve, reject) {
      if (that.data.index === "" || that.data.index == undefined) {
        wx.showToast({
          title: "请选择顶级分类",
          icon: 'none',
          duration: 2000,
        })
      }else if (that.data.child_index === "" || that.data.child_index == undefined) {
        wx.showToast({
          title: "请选择子级分类",
          icon: 'none',
          duration: 2000,
        })
      }
      else{
        var arr=that.data.provinceList[that.data.index].children
        var one_selid=arr[that.data.child_index].xhycategorizeId
        //var two_selid=arr[that.data.child_index].children?arr[that.data.child_index].children[that.data.childs_index].xhycategorizeId:0
      wx.request({
              url: api.admin.xhywinningSave,
              data: {
                xhyactivityId: that.data.xhyactivityId,
                xhymembersId: that.data.xhymembersId,
                xhyprizesId: that.data.xhyprizesId,
                xhylevelId: that.data.xhylevelId,
                xhywinningName: that.data.name,
                xhywinningPhone: that.data.phone,
                xhycategorizeId: one_selid,
                isTicket:0,
                isXhywinningdel: 0
              },
              header: wx.getStorageSync('header'),
              method: 'POST',
              dataType: 'json',
              success: (result) => {
                resolve(result)
                console.log("result", result.data)
                if (result.data.success == "保存成功!") {
                  // wx.showToast({
                  //   title: result.data.success,
                  //   icon: 'success',
                  //   duration: 2000
                  // }) 

                  wx.navigateTo({
                    url: '../winner_submit_tips/winner_submit_tips'                                
                  })
                
                } else {
                  wx.showToast({
                    title: result.data.error,
                    icon: 'none',
                    image: '',
                    duration: 2500,
                    mask: false,
                    success: (result) => {

                    },
                    fail: () => { },
                    complete: () => { }
                  });
                }

              },
              fail: () => {
              },
              complete: () => { }
            })
            }

      
    })
    return p
  },


})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值