小程序三级联动(动态获取数据)

做了两个小时才发现。。。不适合文字太多的联动,没办法。只能改成三个选择框的那种。在这里记录一下是如何做的,然后以后有用时再用。

js

const app = getApp();
Page({
  data: {
    index: null,
    picker: [‘喵喵喵‘, ‘汪汪汪‘, ‘哼唧哼唧‘],
    multiNameArray: [
      [‘请选择‘,‘无脊柱动物‘, ‘脊柱动物‘],
      [‘请选择‘,‘一年级‘,‘二年级‘],
      [‘请选择‘,‘1班‘,‘2班‘, ‘3班‘]
    ],
    muliArray:[[],[],[]],
    multiIndex: [0, 0, 0]
  },
  onLoad(){
    let that=this;
    var data = {
      multiNameArray: that.data.multiNameArray,
      muliArray: that.data.muliArray
      }
    wx.request({
      url: app.globalData.root + ‘service/WeChatParent.asmx/GetOnLoadSchoolList‘,
      method: ‘post‘,
      success: function (datas) {
        console.log(datas);
        var resu = JSON.parse(datas.data.d);
        data.multiNameArray[0] = resu.nameList;
        data.muliArray[0]=resu.list;
        data.muliArray[1] = resu.gradelist;
        data.multiNameArray[1] = resu.gradenamelist;
        data.muliArray[2] = resu.classlist;
        data.multiNameArray[2] = resu.classname;
        that.setData(data)
      }
    })
  },
  PickerChange(e) {
    console.log(e);

    this.setData({
      index: e.detail.value
    })
  },
  MultiChange (e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },
  MultiColumnChange(e) {
    let that=this;

    let multiIndex = that.data.multiIndex;
    let data = {
      multiNameArray: that.data.multiNameArray,
      muliArray: that.data.muliArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    console.log(e.detail);

    var sid = that.data.muliArray[0][multiIndex[0]].Id;
    var gid = that.data.muliArray[1][multiIndex[1]].Id;
    console.log(sid+"|"+gid);

    wx.request({
      url: app.globalData.root + ‘service/WeChatParent.asmx/GetSchoolInfoList‘,
        method: ‘post‘,
      data: {
        colId: e.detail.column,
        schoolId: sid,
        greadeId: gid
        },
        success: function (datas) {
          console.log(datas);
          var resu = JSON.parse(datas.data.d);
          switch (e.detail.column) {
            case 0:
              that.setData({ ‘multiIndex[1]‘: 0 })
              that.setData({
                ‘multiNameArray[1]‘: resu.namelist,
                ‘muliArray[1]‘: resu.list
               })
              break;
            case 1:
              that.setData({
                ‘multiNameArray[2]‘: resu.namelist,
                ‘muliArray[2]‘: resu.list
              })
              break;
            // case 2:
            //   that.setData({
            //     ‘multiNameArray[2]‘: resu.namelist,
            //     ‘muliArray[2]‘: resu.list
            //   })
            //   break;
          }
        }
      })

    // switch (e.detail.column) {
    //   case 0:
    //     that.setData({ ‘multiIndex[0]‘: e.detail.value })
    //     break;
    //   case 1:
    //     that.setData({ ‘multiIndex[1]‘: e.detail.value })
    //     break;
    //   case 2:
    //     that.setData({ ‘multiIndex[2]‘: e.detail.value })
    //     break;
    // }
    // this.setData(data);
  },
  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  DateChange (e) {
    this.setData({
      date: e.detail.value
    })
  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
})

wxml

<form>

  <view class="cu-form-group ">
    <view class=‘title‘>邮件</view>
    <input placeholder="两字短标题" class=‘radius‘ name=‘input‘></input>
  </view>
  <view class="cu-form-group">
    <view class=‘title‘>输入框</view>
    <input placeholder="三字标题" class=‘radius‘ name=‘input‘></input>
  </view>
  <view class="cu-form-group">
    <view class=‘title‘>收货地址</view>
    <input placeholder="统一标题的宽度" class=‘radius‘ name=‘input‘></input>
  </view>
  <view class="cu-form-group">
    <view class=‘title‘>收货地址</view>
    <input placeholder="输入框带个图标" class=‘radius‘ name=‘input‘></input>
    <text class=‘icon-locationfill text-orange‘></text>
  </view>
  <view class="cu-form-group">
    <view class=‘title‘>验证码</view>
    <input placeholder="输入框带个按钮" class=‘radius‘ name=‘input‘></input>
    <button class=‘cu-btn bg-green shadow‘>验证码</button>
  </view>
  <view class="cu-form-group">
    <view class=‘title‘>手机号码</view>
    <input placeholder="输入框带标签" class=‘radius‘ name=‘input‘></input>
    <view class="cu-capsule radius">
      <view class=‘cu-tag bg-blue ‘>
        +86
      </view>
      <view class="cu-tag line-blue">
        河北沧州
      </view>
    </view>
  </view>

  <view class="cu-form-group margin-top">
    <view class=‘title‘>普通选择</view>
    <picker bindchange="PickerChange" value="{{index}}" range="{{picker}}">
      <view class="picker">
        {{index?picker[index]:‘禁止换行,超出容器部分会以 ... 方式截断‘}}
      </view>
    </picker>
  </view>
  <view class="cu-form-group">
    <view class=‘title‘>多列选择</view>
    <picker mode="multiSelector" bindchange="MultiChange" bindcolumnchange="MultiColumnChange" value="{{multiIndex}}" range="{{multiNameArray}}">
      <view class="picker">
        {{multiArray[0][multiIndex[0]]}},{{multiArray[1][multiIndex[1]]}},{{multiArray[2][multiIndex[2]]}}
      </view>
    </picker>
  </view>
<view class="padding flex flex-direction">
  <button class=‘cu-btn bg-green margin-tb-sm lg‘>提交</button>
</view>

<!--
  <view class="cu-form-group margin-top">
    <view class=‘title‘>多级联动</view>
    <picker mode="region" bindchange="RegionChange" value="{{region}}" custom-item="{{customItem}}">
      <view class="picker">
        {{region[0]}},{{region[1]}},{{region[2]}}
      </view>
    </picker>
  </view> -->
</form>

    [WebMethod(Description = "获取学校列表")]
        public string GetOnLoadSchoolList()
        {
            var list = new List<SchoolModel>();
            var gradeList= new List<SchoolModel>();
            var classList= new List<SchoolModel>();

            var schooLlist = ta_School.GetModelList("IsDelete", false);
            var nameList=new List<string>();
            var gradeNameList = new List<string>();
            var classNameList = new List<string>();
            foreach (var item in schooLlist)
            {
                list.Add(new SchoolModel{Id=item.ID,Name=item.Name});
                nameList.Add(item.Name);
            }

            var schoolId = schooLlist.First().ID;
            var gradeLists = PublicQuery.GetGradeListBySchoolId(schoolId);
            foreach (var item in gradeLists)
            {
                gradeList.Add(new SchoolModel { Id = item.ID, Name = item.Name });
                gradeNameList.Add(item.Name);
            }

            var gradeId = gradeLists.First().ID;
            var classs = PublicQuery.GetClassByGreadeId(schoolId,gradeId);
            foreach (var item in classs)
            {
                classList.Add(new SchoolModel { Id = item.ID, Name = item.Name });
                classNameList.Add(item.Name);
            }

            return new { list = list, nameList = nameList,gradelist=gradeList,gradenamelist=gradeNameList,classlist=classs,classname=classNameList }.ToJson();
        }

        [WebMethod(Description = "获取学校列表")]
        public string GetSchoolInfoList(int colId,int schoolId,int greadeId)
        {
            var list = new List<SchoolModel>();
            var nameList = new List<string>();
            switch (colId)
            {
                case 0:
                    var gradelist=PublicQuery.GetGradeListBySchoolId(schoolId);
                    list.Add(new SchoolModel{Id=0,Name="请选择"});
                    nameList.Add("请选择");
                    foreach (var item in gradelist)
                    {
                    list.Add(new SchoolModel{Id= item.ID, Name= item .Name});
                    nameList.Add(item.Name);
                    }
                    break;
                case 1:
                    var classs = PublicQuery.GetClassByGreadeId(schoolId, greadeId);
                    list.Add(new SchoolModel { Id = 0, Name = "请选择" });
                    nameList.Add("请选择");
                    foreach (var item in classs)
                    {
                        list.Add(new SchoolModel { Id = item.ID, Name = item.Name });
                        nameList.Add(item.Name);
                    }
                    break;
                case 2:

                    break;
            }

            return new { list = list, namelist = nameList }.ToJson();
        }

        public class SchoolModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

C#

原文地址:https://www.cnblogs.com/axu92312/p/10523046.html

时间: 2024-10-13 11:28:34

小程序三级联动(动态获取数据)的相关文章

Jquery三级联动(需要JSON数据)

Category.js $(function () { BindCategory(); }) function BindCategory() { var $txtCategory = $("#txtCategoryId"); var _selectId = ["selBigClass", "selSmallClass", "selThreeClass"]; for (var i = 0; i < _selectId.le

微信小程序后台返回大量多余数据的处理

台接口返回一个数组,数组里面N多对象,每个对象里面几十上百条数据,最好玩的是,我只需要每个对象里面的某两个数据.... 类似这种: datas:[   {   id:1000,   name: "帅哥",   title: '...',   b: '...',   d: 0,   f:0,   ....   },   {   id:1001,   name: "美女",   title: '...',   b: '...',   d: 0,   f:0,   ...

highcharts动态获取数据生成图表问题

动态获取数据说白点就是从后台传值到前台,前台把这些值赋值给x轴与y轴.  柱状图的动态传值: //获取后台数据 var x = [];//X轴 var y = [];//Y轴 var xtext = [];//X轴TEXT var color = ["#FFB6C1","#DC143C","#C71585","#D8BFD8","#8B008B","#4B0082","#6A5

三级联动,json数据、可设置默认城市

闲来无事,折腾个三级联动,json数据.可设置默认城市.可配置是否显示第三级select <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>省市县三级联动</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script&g

小程序踩坑:获取图片id后显示不成功

.wxml 写一个bindtap事件,wx:for 循环列表,id是小程序自带的获取列表中图片的id, index.js 特别注意url路径, 错误写法 注意:第一个id后不能有空格,如果像上图所示加了空格,那么创建的将不是“id”而是"id ",在显示id时也会一直提示Setting data field "id" to undefined is invalid. 正常获取后: wxml页面就能读取到啦 原文地址:https://www.cnblogs.com/t

微信小程序首页index.js获取不到app.js中动态设置的globalData的原因以及解决方法

前段时间开发了一款微信小程序,运行了也几个月了,在index.js中的onLoad生命周期里获取app.js中onLaunch生命周期中在接口里动态设置的globalData一直没有问题,结果昨天就获取不到动态设置的global数据了.因为我要获取小程序的unionid以及通过unionid去获取微信的openId,并且共享数据,最开始我是通过storage来做数据共享的,后来测试发现storage存在莫名被清空的情况,后来转用globalData来做数据共享,查了下,是因为app.js中的on

微信小程序省市联动

最近呢刚好做了一个省市联动的功能,今天看到有人问这个怎么做,我就把我做的放上来共享一下: 首先呢,来看看效果,点击文字'点击',弹出选择窗口,点击取消或者确定(取消.确定按钮在选择框上边,截图有些不清楚),窗口下滑, 做这个我用的是picker-view这个组件,现在来看一看picker-view的属性: 现在开始写wxml的代码,对了,插一句,我这里是把它写成一个模板的,先看看目录结构 我们先来看看cascade.wxml里的代码: <template name="cascade&quo

.NET开发微信小程序-接收微信支付回调数据

获取微信支付传回来的数据 1.MVC控制器 /// <summary> /// 付款返回的数据 /// </summary> /// <returns></returns> public string Notify_url() { String xmlData = getPostStr(); //保存付款成功过来的数据 Log4Helper.ErrorInfo("GXL", xmlData); WxAPIService.PayResult

React使用jquery方式动态获取数据

今天用了一下最简单的方式获取数据处理,来结合react实现实时请求,并刷新数据的小demo. 首先我还是选择了jquery方式中自带的ajax获取数据,首先要引用所需的js包 接下来要写一个自定义的js文件, 创建一个react组件: var Demo = React.createClass({}); //渲染组件 React.render(<Demo url="../data/package.json" setTime="2000" />,docume