uniapp 多级列表展开选择组件

1、多级列表
2、展开缩起
3、效果图


截屏2021-07-06 下午1.53.51.png

代码如下:

<template>
    <view class="content">
        <checkbox-group @change="checkboxChange">
            <scroll-view class="position-group" :style="getHeight()">
                <!-- 一级循环 -->
                <view class="pronames_one" v-for="(item, index) in positionlist" :key="index"
                    :class="item.curSelect?'oneshow':'onehide'">
                    <view class="pronames-one-label" @click="onelist(index)">
                        <view class="item-v">
                            <view v-if="getHasChildren(item[defaultKey.children])"
                                :class="item.curSelect?'iconfont icon-jian icon-v':'iconfont icon-ziyuan icon-v'">
                            </view>
                            <text>{{getTitle(item)}}</text>
                            <label :class="item.curSelect?'cuIcon-fold':'cuIcon-unfold'"></label>
                        </view>
                        <checkbox class="t-v" v-if='getShowCheckbox(item)' :value="getItem(item)"
                            :checked="item.curSelect" style="transform:scale(0.85)" />
                    </view>
                    <!-- 二级循环 -->
                    <view class="pronames_two" v-for="(item2, index2) in item[defaultKey.children]" :key="index2"
                        :class="item2.curSelect?'twoshow':'twohide'">
                        <view class="pronames-two-label" @click="twolist(index,index2)">
                            <view class="item-v">
                                <view v-if="getHasChildren(item2[defaultKey.children])"
                                    :class="item2.curSelect?'iconfont icon-jian icon-v':'iconfont icon-ziyuan icon-v'">
                                </view>
                                <text>{{getTitle(item2)}}</text>
                                <label :class="item2.curSelect?'cuIcon-fold':'cuIcon-unfold'"></label>
                            </view>
                            <checkbox class="t-v" v-if='getShowCheckbox(item2)' :value="getItem(item2)"
                                :checked="item2.curSelect" style="transform:scale(0.85)" />
                        </view>
                        <!-- 三级循环 -->
                        <view class="pronames_three" v-for="(item3, index3) in item2[defaultKey.children]" :key="index3"
                            :class="item3.curSelect?'threeshow':'threehide'">
                            <view class="pronames_three-label" @click="threelist(index,index2, index3)">
                                <view class="item-v">
                                    <view v-if="getHasChildren(item3[defaultKey.children])"
                                        :class="item3.curSelect?'iconfont icon-jian icon-v':'iconfont icon-ziyuan icon-v'">
                                    </view>
                                    <text>{{getTitle(item3)}}</text>
                                    <label :class="item3.curSelect?'cuIcon-fold':'cuIcon-unfold'"></label>
                                </view>
                                <checkbox class="t-v" v-if='getShowCheckbox(item3)' :value="getItem(item3)"
                                    :checked="item3.curSelect" style="transform:scale(0.85)" />
                            </view>
                            <view class="pronames_four" v-for="(item4, index4) in item3[defaultKey.children]"
                                :key="index4">
                                <!-- 4级循环 -->
                                <view class="pronames_four-label" @click="fourListClick(index,index2, index3, index4)">
                                    <view class="item-v">
                                        <text>{{getTitle(item4)}}</text>
                                        <label :class="positionSele?'cuIcon-round':'cuIcon-roundcheckfill'"></label>
                                    </view>
                                    <checkbox class="t-v" :value="getItem(item4)" :checked="item4.curSelect"
                                        style="transform:scale(0.85)" />
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </scroll-view>
        </checkbox-group>
        <view class="bottom-v">
            <button type="default" class="debtn-v" @click.stop="deleteBtn(positionlist)">清空</button>
            <button type="primary" class="btn-v" @click.stop="surekBtn()">确定</button>
        </view>
    </view>
</template>
<script>
    export default {
        props: {
            positionlist: {
                type: Array,
                default () {
                    return []
                }
            },
            keyProps: {
                type: Object,
                default () {
                    return {}
                }
            }
        },
        data() {
            return {
                positionSele: true,
                defaultKey: {
                    title: 'title',
                    value: 'id',
                    children: 'children'
                },
                selectList: []
            }
        },
        mounted() {
            for (let key in this.keyProps) {
                this.defaultKey[key] = this.keyProps[key]
            }
        },
        computed: {
            getItem() {
                return function(data) {
                    return JSON.stringify(data)
                }
            },
            getHeight() {
                return function() {
                    const systemInfo = uni.getSystemInfoSync()
                    let h = systemInfo.windowHeight - uni.upx2px(120)
                    return {
                        height: h
                    }
                }
            },
            getTitle() {
                return function(item) {
                    
                    return item[this.defaultKey.title]
                }
            },
            getShowCheckbox() {
                return function(item) {
                    let chlidren = item[this.defaultKey.children]
                    if (chlidren && chlidren.length == 0) {
                        return true
                    }
                }
            },
            getHasChildren() {
                return function(data) {
                    if (data && data.length > 0) {
                        return true
                    }
                    return false
                }
            },
        },
        methods: {
            checkboxChange(e) {
                
            },
            deleteBtn(dataList) {
                for (let i = 0; i < dataList.length; i++) {
                    let data = dataList[I]
                    let children = data[this.defaultKey.children]
                    data.curSelect = false
                    if (children&&children.length > 0) {
                        this.deleteBtn(children)
                    }
                }
            },
            findSelect(dataList) {
                for (let i = 0; i < dataList.length; i++) {
                    let data = dataList[I]
                    let children = data[this.defaultKey.children]
                    if (data.curSelect && (!children || children.length == 0)) {
                        this.selectList.push(data)
                    }
                    else if (children&&children.length > 0) {
                        this.findSelect(children)
                    }
                }
            },
            surekBtn() {
                this.selectList = []
                this.findSelect(this.positionlist)
                // console.log('selectList', this.selectList)
                this.$emit('selectEnd', this.selectList, this.positionlist)
                // console.log('positionlist', this.positionlist)
            },

            onelist(index) {
                let positionlist = this.positionlist;
                if (!positionlist[index]['curSelect']) {
                    positionlist[index]['curSelect'] = true;
                } else {
                    positionlist[index]['curSelect'] = false;
                }
            },

            twolist(index, index2) {

                let children = this.positionlist[index][this.defaultKey.children][index2]
                let select = children['curSelect']
                if (!select) {
                    children['curSelect'] = true;
                } else {
                    children['curSelect'] = false;
                }
            },
            threelist(index, index2, index3) {
                let children = this.positionlist[index][this.defaultKey.children][index2][this.defaultKey.children][index3]
                let select = children['curSelect']
                if (!select) {
                    children['curSelect'] = true;
                } else {
                    children['curSelect'] = false;
                }
            },
            fourListClick(index, index2, index3, index4) {
                let children = this.positionlist[index][this.defaultKey.children][index2][this.defaultKey.children][index3]
                    [this.defaultKey.children][index4]
                let select = children['curSelect']
                if (!select) {
                    children['curSelect'] = true;
                } else {
                    children['curSelect'] = false;
                }
            }
        }
    }
</script>

<style>
    page {
        background: #fff;
    }

    .position-group {
        width: 100%;
        height: auto;
    }

    .icon-v {
        padding: 0 10rpx 0 0rpx;
        color: #F05B38;
    }

    .item-v {
        display: flex;
        flex-direction: row;
        align-items: center;
    }

    .bottom-v {
        background-color: #FFFFFF;
        height: 100rpx;
        border-top: 2rpx solid #E5E5E5;
        position: fixed;
        bottom: 0;
        display: flex;
        flex-direction: row;
        align-items: center;
        width: 100%;
    }
    .debtn-v {
        width: 160rpx;
        line-height: 80rpx;
        font-size: 26rpx;
    }
    .btn-v {
        width: 60%;
        line-height: 80rpx;
        font-size: 26rpx;
    }

    /* 一级 */
    .pronames_one {
        width: 100%;
        height: auto;
    }

    .pronames-one-label {
        width: 100%;
        height: 80rpx;
        line-height: 80rpx;
        background: #fff;
        box-sizing: border-box;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: 0 20rpx;
        border-bottom: 1px #eaeaea solid;
    }

    .pronames-one-label text {
        font-size: 30rpx;
        color: #2ebbfe;
        color: #333333;
    }

    .pronames-one-label label {
        color: #aaa;
        font-size: 40rpx;
        transition: all 0.5s;
    }

    /* 二级 */
    .pronames_two {
        width: 100%;
        height: auto;
        transition: all 0.5s;
    }

    .pronames-two-label {
        margin-left: 40rpx;
        width: calc(100% - 40rpx);
        height: 80rpx;
        line-height: 80rpx;
        display: flex;
        flex-direction: row;
        box-sizing: border-box;
        justify-content: space-between;
        padding: 0 10rpx;
        border-bottom: 1px #eee solid;
    }

    .pronames-two-label text {
        font-size: 28rpx;
        color: #666666;
        /* margin-left: 40rpx; */
    }

    .pronames-two-label label {
        color: #aaa;
        font-size: 20px;
        transition: all 0.5s;
    }

    /* 三级 */
    .pronames_three {
        width: 100%;
        height: auto;
        transition: all 0.5s;
    }

    .pronames_three-label {
        margin-left: 80rpx;
        width: calc(100% - 80rpx);
        height: 80rpx;
        line-height: 80rpx;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 0 10rpx;
        border-bottom: 1px #eee solid;
    }

    .pronames_three-label text {
        font-size: 26rpx;
        color: #666666;
        /* margin-left: 80rpx; */
    }

    .pronames_three-label label {
        color: #aaa;
        font-size: 20px;
        transition: all 0.5s;
    }

    /* .pronames_three-label label {
        height: 90rpx;
        line-height: 90rpx;
        display: block;
        color: #D5D5D5;
        font-size: 20px;
    } */

    /* 三级 */
    .pronames_four {
        width: 100%;
        height: auto;
    }

    .pronames_four-label {
        margin-left: 120rpx;
        width: calc(100% - 120rpx);
        height: 80rpx;
        line-height: 80rpx;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        box-sizing: border-box;
        padding: 0 10rpx;
        border-bottom: 1px #eee solid;
        /* background-color: #007AFF; */
    }

    .pronames_four-label label {
        /* width: 100%; */
        height: 90rpx;
        line-height: 90rpx;
        display: block;
        color: #D5D5D5;
        font-size: 20px;
    }

    .pronames_four-label text {
        /* width: 100%; */
        font-size: 26rpx;
        color: #666666;
        /* margin-left: 80rpx; */
        /* background-color: #007AFF; */
    }

    /* 展开收起效果 start */

    .oneshow {}

    .oneshow .pronames_two {
        display: block;
    }

    .onehide {}

    .onehide .pronames_two {
        display: none;
    }

    /* 展开收起效果 end */


    .twoshow {}

    .twoshow .pronames_three {
        display: block;
    }

    .twohide {}

    .twohide .pronames_three {
        display: none;
    }

    .threeshow {}

    .threeshow .pronames_four {
        display: block;
    }

    .threehide {}

    .threehide .pronames_four {
        display: none;
    }
</style>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351

推荐阅读更多精彩内容