小程序二级联动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<picker mode="multiSelector" range="{{ edit_cateArray_temp }}" range-key="name" value="{{ edit_cateSelectIndex }}" bindchange="changeCate" bindcolumnchange="bindMultiCateColumnChange">
         <view class="flex j-s a-c box-border padding-primary" style="padding-left: 0;padding-top: 0;">
           <view>所属类别</view>
           <view class="flex a-c j-s">
             <view wx:if="{{ edit_cateSelectIndex !== null }}" class="flex-1 pl-10 color-555">
               {{edit_cateArray[0][edit_cateSelectIndex[0]].name}}
               {{edit_cateArray[1][edit_cateSelectIndex[1]].name}}
             </view>
              
             <view wx:else class="placeholder-class fc pl-10">点击选择</view>
             <pic class="icon-input-arrow-right" src="/assets/images/common/icon_nav_right.png" />
           </view>
         </view>
       </picker>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Router({
 
    /**
     * 页面的初始数据
     */
    data: {
         
        allCates: [], //所有分类
          
        edit_cateArray: [
            [],
            []
        ], //编辑可选值
        edit_cateArray_temp: [
            [],
            []
        ], //编辑可选值,此源用于选择,因为要在没确定选择之前不需要更新edit_cateArray
        edit_cateSelectIndex: null, //编辑选中分类,同样是数组,第一个值对应第一级分类...
        
 
    },
  
    //加载详情
    loadDetail: function() {
        const that = this;
        getEventDetail({ id: this.data.id }).then(function(detail) {
  
            //分类相关
            const cates = detail.cates || [];
            let cateArray = [
                [],
                []
            ];
            let parentCates = cates.filter(t => t.parentId == null || t.parentId == 0) || [];
 
            cateArray[0] = parentCates.map(c => { return { id: c.id, name: c.name } }) || [];
            if (parentCates.length > 0) {
                let childCates = cates.filter(t => t.parentId == parentCates[0].id) || [];
                cateArray[1] = childCates.map(c => { return { id: c.id, name: c.name } }) || [];
            }
 
            that.$sd("edit_cateArray", cateArray);
            that.$sd("edit_cateArray_temp", cateArray);
 
            //初始选中分类
            const cateSelectIndex = [];
            const parentCateId = detailResult.eventCateParentId || 0;
            if (parentCateId > 0) {
                cateSelectIndex[0] = cateArray[0].map(x => x.id).indexOf(parentCateId);
                if ((detailResult.eventCateId || 0) > 0) {
                    let childCates = cates.filter(t => t.parentId == parentCateId) || [];
                    cateArray[1] = childCates.map(c => { return { id: c.id, name: c.name } }) || [];
                    cateSelectIndex[1] = childCates.map(x => x.id).indexOf(detailResult.eventCateId);
                }
                that.$sd("edit_cateArray", cateArray);
                that.$sd("edit_cateArray_temp", cateArray);
                that.$sd("edit_cateSelectIndex", cateSelectIndex);
            }
        })
    }, 
    changeCate: function(e) {
        //确定选定分类
        this.$sd("edit_cateArray", this.data.edit_cateArray_temp);
        const index = e.detail.value;
        this.$sd("edit_cateSelectIndex", index);
    },
    bindMultiCateColumnChange: function(e) {
        const column = e.detail.column;
        const val = e.detail.value
        let cateArray = this.data.edit_cateArray_temp;
 
        if (column == 0) {
            var firstSelectCateId = cateArray[0][val].id;
            let childCates = this.data.allCates.filter(t => t.parentId == firstSelectCateId) || [];
            cateArray[1] = childCates.map(c => { return { id: c.id, name: c.name } }) || [];
 
            this.$sd("edit_cateArray_temp", cateArray);
        } else if (column == 1) {
            //no
        }
    
 
})

  

posted @   evemen  阅读(529)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示