三级联动

    xiaoxiao2022-07-02  101

    js代码

    Page({

      /**    * 页面的初始数据    */   data: {     objectMultiShow: [],     objectMultiArray: [],     multiArray: [],     multiIndex: [],     checkeIndex: []   },

      /**    * 生命周期函数--监听页面加载    * objectMultiShow--匹配多列选择器渲染数据所对应的json数据(包含id,parentId,name)    * multiArray--多列选择器渲染数据    */   onLoad: function(options) {     // 初始化     let data = {       objectMultiShow: this.data.objectMultiShow,       objectMultiArray: this.data.objectMultiArray,       multiArray: this.data.multiArray,       multiIndex: this.data.multiIndex,       checkeIndex: this.data.checkeIndex     }     data.objectMultiArray = [       [         {  parentKey: 7, key: 26, name: "智慧大厦" }],       [{ parentKey: 26, key: 27, name: "9F" },         { parentKey: 26, key: 28, name: "12F" },         {  parentKey: 26, key: 29, name: "15F" },

            {  parentKey: 26, key: 30, name: "11F" }       ],       [{ parentKey:29, key: 31, name: "1501F" },         { parentKey: 29, key: 32, name: "1502F" },         {  parentKey: 29, key: 33, name: "1503F" },

            {  parentKey: 29, key: 34, name: "1504F" }       ]

        ]     data.objectMultiShow = data.objectMultiArray.map((item, index) => {       if (index > 0) {         item = item.filter(i => i.parentKey === data.objectMultiArray[index - 1][0].key)       }       return item     })     data.multiArray = data.objectMultiShow.map(item => {       item = item.map(i => i.name)       return item     })     console.log(data.multiIndex)     // 数据更新     this.setData(data)   },

      bindMultiPickerChange: function(e) {     console.log('picker发送选择改变,携带值为', e.detail.value)     this.setData({       multiIndex: e.detail.value     })   },   bindMultiPickerColumnChange: function(e) {     console.log('修改的列为', e.detail.column, ',值为', e.detail.value);     // 初始化数据     var data = {       objectMultiShow: this.data.objectMultiShow,       multiArray: this.data.multiArray,       multiIndex: this.data.multiIndex     };

        // 改变第i列数据之后,后几列选择第0个选项(重置)     data.multiIndex[e.detail.column] = e.detail.value;     for (let i = e.detail.column; i < data.multiIndex.length - 1; i++) {       data.multiIndex[i + 1] = 0     }

        /**      * 改变第i列数据之后,后几列数据更新      * 两种写法:for 和 switch,switch为三列选择器写法,for由switch精简拓展过来,可用于多列选择器      * swich写法:如果更改的是第一列数据,第二列数据通过filter筛选(parentId = 第一列选中项id),同时更新第三列数据      *            如果更改的是第二列数据,更新第三列数据 通过filter筛选(parentId = 第二列选中项id)      */     let arry = this.data.objectMultiArray     for (let i = e.detail.column; i < data.multiIndex.length - 1; i++) {       data.objectMultiShow[i + 1] = arry[i + 1].filter(item => item.parentKey === data.objectMultiShow[i][data.multiIndex[i]].key)       data.multiArray[i + 1] = data.objectMultiShow[i + 1].map(item => item.name)     }     /*switch (e.detail.column) {       case 0:         data.objectMultiShow[1] = arry[1].filter(item => item.parentId === data.objectMultiShow[0][data.multiIndex[0]].id)         data.multiArray[1] = data.objectMultiShow[1].map(item => item.name)         data.objectMultiShow[2] = arry[2].filter(item => item.parentId === data.objectMultiShow[1][data.multiIndex[1]].id)         data.multiArray[2] = data.objectMultiShow[2].map(item => item.name)         break;       case 1:         data.objectMultiShow[2] = arry[2].filter(item => item.parentId === data.objectMultiShow[1][data.multiIndex[1]].id)         data.multiArray[2] = data.objectMultiShow[2].map(item => item.name)     }*/     // 数据更新     this.setData(data);   } })

    wxml代码:

    <view class="container">   <form catchsubmit="formSubmit">     <view class="form-card">       <view class="weui-cell weui-cell_input">         <view class="weui-cell__hd">           <view class="weui-label">楼<span>*</span></view>         </view>         <view class="weui-cell__bd">           <picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{{multiIndex}}" range="{{multiArray}}">             <view class="weui-input">选择:{{multiArray[0][multiIndex[0]]}}{{multiArray[1][multiIndex[1]]}}{{multiArray[2][multiIndex[2]]}}</view>           </picker>         </view>       </view>     </view>   </form>    </view>  

    最新回复(0)