无限树状列表的实现

树状列表示例效果.png

1. 无限级树形组件1

(1) 组件源码 tree-item.vue

<template>  
    <view>  
    <view class="tree-ul" v-for="(item,i) in data" :key="item.code+i">  
        <view class="isTrue" v-if="item.children">                
            <view class="tree-item">  
                <label>{{item.name}}</label>  
                <span v-if="!opends[i]" @tap="onOpends(item,i)">&or;</span>  
                <span v-if="opends[i]"  @tap="onOpends(item,i)">&and;</span>  
            </view>  
            <tree-item class="children-box" :data="item.children" v-show="opends[i]"></tree-item>  
        </view>  
        <view class="tree-item isTrue"  v-if="!item.children">  
            <label>{{item.name}}</label>  
        </view>  
    </view></view>  
</template>  

<script>  
    export default {  
        name: "tree-item",  
        props:{  
            data: {  
                type: Array,  
                default: function(e) {  
                    return []  
                }  
            }  
        },  
        data() {  
            return {  
                opends: [],  
            };  
        },  
        onLoad() {  
            console.log("tree-item onLoad")
        },  
        methods:{  
            onOpends(item,i){  
                this.$nextTick(function(){  
                    this.$set(this.opends,i,!this.opends[i])  
                })  
            }  
        }  
    }  
</script>  

<style>  
    .tree-box{  
        display: inline-block;  
        width: 220px;  
        border: 1px solid #eee;  
        overflow: hidden;  
        border-radius: 4px;  
    }  
    .tree-item{  
        display: flex;  
        overflow: hidden;  
        height: 32px;  
        border-bottom: 1px solid #eee;  
    }  
    .tree-item>label{  
        flex: 1;  
        line-height: 32px;  
        font-size: 14px;  
        overflow: hidden;  
        text-overflow: ellipsis;  
        white-space: nowrap;  
    }  
    .tree-item>span{  
        width: 32px;  
        height: 32px;  
        text-align: center;  
        line-height: 32px;  
    }  
    .isTrue{  
        padding-left: 15px;  
    }  
</style>  

(2) index.vue中调用

<template>
    <view>
        <view>该组件只能在h5端实现树状列表;无法实现微信小程序和android端的树状列表,只能显示一级而无法展开下级列表</view>
        <view position="middle" mode="fixed" style="background-color: #FFFFFF;">
            <scroll-view class="uni-bg-white" style="height:500upx;width:300upx" scroll-y="true">
                <tree-folder :data="lists"></tree-folder>
            </scroll-view>
        </view>
    </view>
</template>

<script>
    import treeFolder from "@/components/tree/tree-item.vue";
    
    export default {
        components: {
            treeFolder
        },
        data() {
            return {
                lists:[
                    {
                        name:"椒江新厂",
                        code:"001",
                        children:[
                            {
                                name:"生产部门A",
                                code:"021",
                                children:[
                                    {
                                        name:"A-01",
                                        code:"031",
                                    },
                                    {
                                        name:"A-02",
                                        code:"032",
                                    },
                                    {
                                        name:"A-03",
                                        code:"033",
                                    }
                                ]
                            },
                            {
                                name:"生产部门B",
                                code:"022",
                                children:[
                                    {
                                        name:"B-01",
                                        code:"034",
                                    }
                                ]
                            },
                            {
                                name:"生产部门C",
                                code:"023",
                            }
                        ]
                    },
                    {
                        name:"杭州工厂",
                        code:"002",
                    },
                    {
                        name:"西安工厂",
                        code:"003",
                    }
                ]
            }
        },
        methods: {
            
        }
    }
</script>

<style>
    page {
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        background-color: #fff;
    }
</style>


备注:uni-app使用vue递归组件做无限级树形列表时,在H5页面能正常显示(H5兼容递归组件本身),但是在微信小程序和android端只能显示第一级(小程序和安卓app无法递归)。该组件无法实现微信小程序和android端的树状列表,只能显示一级而无法展开下级列表。

2. 无限级树形组件2

(1) 组件源码mix-tree.vue

<template>
    <view class="content">
        <view class="mix-tree-list">
            <block v-for="(item, index) in treeList" :key="index">
                <view 
                    class="mix-tree-item"
                    :style="[{
                            paddingLeft: item.rank*15 + 'px',
                            zIndex: item.rank*-1 +50
                        }]"
                    :class="{
                            border: treeParams.border === true,
                            show: item.show,
                            last: item.lastRank,
                            showchild: item.showChild
                        }"
                    @click.stop="treeItemTap(item, index)"
                >
                    <image class="mix-tree-icon" :src="item.lastRank ? treeParams.lastIcon : item.showChild ? treeParams.currentIcon : treeParams.defaultIcon"></image>
                    {{item.name}}
                </view>
            </block>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            list: {
                type: Array,
                default(){
                    return [];
                }
            },
            params: {
                type: Object,
                default(){
                    return {}
                }
            }
        },
        data() {
            return {
                treeList: [],
                treeParams: {
                    defaultIcon: '/static/mix-tree/defaultIcon.png',
                    currentIcon: '/static/mix-tree/currentIcon.png',
                    lastIcon: '',
                    border: false
                }
            }
        },
        watch: {
            list(list){
                
                this.treeParams = Object.assign(this.treeParams, this.params);
                console.log(this.treeParams, this.params);
                this.renderTreeList(list);
            }
        },
        onLoad() {  
            console.log("mix-tree onLoad")
        }, 
        methods: {
            //扁平化树结构
            renderTreeList(list=[], rank=0, parentId=[]){
                list.forEach(item=>{
                    this.treeList.push({
                        id: item.id,
                        name: item.name,
                        parentId,  // 父级id数组
                        rank,  // 层级
                        showChild: false,  //子级是否显示
                        show: rank === 0  // 自身是否显示
                    })
                    if(Array.isArray(item.children) && item.children.length > 0){
                        let parents = [...parentId];
                        parents.push(item.id);
                        this.renderTreeList(item.children, rank+1, parents);
                    }else{
                        this.treeList[this.treeList.length-1].lastRank = true;
                    }
                })
            },
            // 点击
            treeItemTap(item){
                let list = this.treeList;
                let id = item.id;
                if(item.lastRank === true){
                    //点击最后一级时触发事件
                    this.$emit('treeItemClick', item);
                    return;
                }
                item.showChild = !item.showChild;
                list.forEach(childItem=>{
                    if(item.showChild === false){
                        //隐藏所有子级
                        if(!childItem.parentId.includes(id)){
                            return;
                        }
                        if(childItem.lastRank !== true){
                            childItem.showChild = false;
                        }
                        childItem.show = false;
                    }else{
                        if(childItem.parentId[childItem.parentId.length-1] === id){
                            childItem.show = true;
                        }
                    }
                })
            }
        }
    }
</script>

<style>
    .mix-tree-list{
        display: flex;
        flex-direction: column;
        padding-left: 30upx;
    }
    .mix-tree-item{
        display: flex;
        align-items: center;
        font-size: 30upx;
        color: #333;
        height: 0;
        opacity: 0;
        transition: .2s;
        position: relative;
    }
    .mix-tree-item.border{
        border-bottom: 1px solid #eee;
    }
    .mix-tree-item.show{
        height: 80upx;
        opacity: 1;
    }
    .mix-tree-icon{
        width: 26upx;
        height: 26upx;
        margin-right: 8upx;
        opacity: .9;
    }
    
    .mix-tree-item.showchild:before{
        transform: rotate(90deg);
    }
    .mix-tree-item.last:before{
        opacity: 0;
    }
</style>

(2) index.vue中调用

<template>
    <view>
        <view>该组件可实现树状列表</view>
        <view position="middle" mode="fixed" style="background-color: #FFFFFF;">
            <scroll-view class="uni-bg-white" style="height:500upx;width:300upx" scroll-y="true">
                <mix-tree :list="lists" @treeItemClick="treeItemClick"></mix-tree>
            </scroll-view>
        </view>
    </view>
</template>

<script>
    import mixTree from '@/components/mix-tree/mix-tree';
    let testList = [
                    {
                        name:"椒江新厂",
                        id:"001",
                        children:[
                            {
                                name:"生产部门A",
                                id:"021",
                                children:[
                                    {
                                        name:"A-01",
                                        id:"031",
                                    },
                                    {
                                        name:"A-02",
                                        id:"032",
                                    },
                                    {
                                        name:"A-03",
                                        id:"033",
                                    }
                                ]
                            },
                            {
                                name:"生产部门B",
                                id:"022",
                                children:[
                                    {
                                        name:"B-01",
                                        id:"034",
                                    }
                                ]
                            },
                            {
                                name:"生产部门C",
                                id:"023",
                            }
                        ]
                    },
                    {
                        name:"杭州工厂",
                        id:"002",
                    },
                    {
                        name:"西安工厂",
                        id:"003",
                    }
                ]
    
    export default {
        components: {
            mixTree
        },
        data() {
            return {
                lists: [],
            }
        },
        onLoad:function(){
            //测试树状列表
            setTimeout(()=>{
                this.lists = testList;
            }, 500);
            console.log(this.lists);
        },
        methods: {
            //点击最后一级时触发该事件
            treeItemClick(item) {
                let {
                    id,
                    name,
                    parentId
                } = item;
                uni.showModal({
                    content: `点击了${parentId.length+1}级菜单, ${name}, id为${id}, 父id为${parentId.toString()}`
                })
                console.log(item)
            }
        }
    }
</script>

<style>

</style>

备注:源码中的id和name字段需根据数据结构定义的字段名进行对应的修改。如网络获取的列表数据为:

 "departmentList": [
    {
      "name": "椒江新厂",
      "id": "001",
      "children": [
        {
          "name": "生产部门A",
          "id": "021",
          "children": [
            {
              "name": "A-01",
              "id": "031"
            },
            {
              "name": "A-02",
              "id": "032"
            },
            {
              "name": "A-03",
              "id": "033"
            }
          ]
        },
        {
          "name": "生产部门B",
          "id": "022",
          "children": [
            {
              "name": "B-01",
              "id": "034"
            }
          ]
        },
        {
          "name": "生产部门C",
          "id": "023"
        }
      ]
    },
    {
      "name": "杭州工厂",
      "id": "002"
    },
    {
      "name": "西安工厂",
      "id": "003"
    }
  ]

则源码mix-tree.vue修改为

<template>
    <view class="content">
        <view class="mix-tree-list">
            <block v-for="(item, index) in treeList" :key="index">
                <view 
                    class="mix-tree-item"
                    :style="[{
                            paddingLeft: item.rank*15 + 'px',
                            zIndex: item.rank*-1 +50
                        }]"
                    :class="{
                            border: treeParams.border === true,
                            show: item.show,
                            last: item.lastRank,
                            showchild: item.showChild
                        }"
                    @click.stop="treeItemTap(item, index)"
                >
                    <image class="mix-tree-icon" :src="item.lastRank ? treeParams.lastIcon : item.showChild ? treeParams.currentIcon : treeParams.defaultIcon"></image>
                    {{item.department_name}}
                </view>
            </block>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            list: {
                type: Array,
                default(){
                    return [];
                }
            },
            params: {
                type: Object,
                default(){
                    return {}
                }
            }
        },
        data() {
            return {
                treeList: [],
                treeParams: {
                    defaultIcon: '/static/mix-tree/defaultIcon.png',
                    currentIcon: '/static/mix-tree/currentIcon.png',
                    lastIcon: '',
                    border: false
                }
            }
        },
        watch: {
            list(list){
                this.treeParams = Object.assign(this.treeParams, this.params);
                // console.log(this.treeParams, this.params);
                this.renderTreeList(list);
            }
        },
        onLoad() {  
            console.log("mix-tree-dep onLoad")
        }, 
        methods: {
            //扁平化树结构
            renderTreeList(list=[], rank=0, parentId=[]){
                //liy:若无该if代码块的判断与处理,网络数据每刷新一次一级目录就会重复一次
                if(rank == 0) {
                    this.treeList = [];
                }
                list.forEach(item=>{
                    this.treeList.push({
                        department_id: item.department_id,
                        department_name: item.department_name,
                        parentId,  // 父级id数组
                        rank,  // 层级
                        showChild: false,  //子级是否显示
                        show: rank === 0  // 自身是否显示
                    })
                    if(Array.isArray(item.children) && item.children.length > 0){
                        let parents = [...parentId];
                        parents.push(item.department_id);
                        this.renderTreeList(item.children, rank+1, parents);
                    }else{
                        this.treeList[this.treeList.length-1].lastRank = true;
                    }
                })
            },
            // 点击
            treeItemTap(item){
                let list = this.treeList;
                let id = item.department_id;
                if(item.lastRank === true){
                    //点击最后一级时触发事件
                    this.$emit('treeItemClick', item);
                    return;
                }
                item.showChild = !item.showChild;
                list.forEach(childItem=>{
                    if(item.showChild === false){
                        //隐藏所有子级
                        if(!childItem.parentId.includes(id)){
                            return;
                        }
                        if(childItem.lastRank !== true){
                            childItem.showChild = false;
                        }
                        childItem.show = false;
                    }else{
                        if(childItem.parentId[childItem.parentId.length-1] === id){
                            childItem.show = true;
                        }
                    }
                })
            }
        }
    }
</script>

<style>
    .mix-tree-list{
        display: flex;
        flex-direction: column;
        padding-left: 30upx;
    }
    .mix-tree-item{
        display: flex;
        align-items: center;
        font-size: 30upx;
        color: #333;
        height: 0;
        opacity: 0;
        transition: .2s;
        position: relative;
    }
    .mix-tree-item.border{
        border-bottom: 1px solid #eee;
    }
    .mix-tree-item.show{
        height: 80upx;
        opacity: 1;
    }
    .mix-tree-icon{
        width: 26upx;
        height: 26upx;
        margin-right: 8upx;
        opacity: .9;
    }
    
    .mix-tree-item.showchild:before{
        transform: rotate(90deg);
    }
    .mix-tree-item.last:before{
        opacity: 0;
    }
</style>

备注:若无该if代码块的判断与处理if(rank == 0) {this.treeList = [];},网络数据每刷新一次一级目录就会重复一次 。
index.vue修改为:

<template>
    <view>
        <view @click="getDepartList">点击此处获取树状列表</view>
        <view position="middle" mode="fixed" style="background-color: #FFFFFF;">
            <scroll-view class="uni-bg-white" style="height:500upx;width:300upx" scroll-y="true">
                <mix-tree :list="departmentList" @treeItemClick="treeItemClick"></mix-tree>
            </scroll-view>
        </view>
    </view>
</template>

<script>
    import mixTree from '@/components/mix-tree/mix-tree';
    
    export default {
        components: {
            mixTree
        },
        data() {
            return {
                departmentList: [],
            }
        },
        onLoad:function(){
            this.getDepartList(); 
        },
        methods: {
                        //网络获取树状列表
            getDepartList() {
                var _this = this;
                uni.request({
                    url: "接口url地址",
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    data: {
                        userName: "liy"
                    },
                    success: res => {
                        if (res.data.result === "1") {
                            _this.departmentList = res.data.departmentList;
                        }
                    },
                    fail: res => {},
                    complete: () => {}
                });
            },
            //点击最后一级时触发该事件
            treeItemClick(item) {
                let {
                    department_id,
                    department_name,
                    parentId
                } = item;
                uni.showModal({
                    content: `点击了${parentId.length+1}级菜单, ${department_name}, id为${department_id}, 父id为${parentId.toString()}`
                })
                console.log(item)
            },
        }
    }
</script>

<style>

</style>

2.1 功能拓展 — 实现选中中间项

页面展示.PNG

说明:第二部分的无限级树形列表点击某个菜单会进行下级菜单的展开或收缩,最终只对最后一级的点击事件进行逻辑处理;若需求是实现每一个菜单项都能被选中,即中间菜单项不仅能实现展开或收缩子菜单,还要能实现当前中间菜单项自身被选中,此时可对第二部分的无限级树形列表的功能进行拓展。源码mix-tree.vue修改为:

<template>
    <view class="content">
        <view class="mix-tree-list">
            <block v-for="(item, index) in treeList" :key="index">
                <view 
                    class="mix-tree-item"
                    :style="[{
                            paddingLeft: item.rank*15 + 'px',
                            zIndex: item.rank*-1 +50
                        }]"
                    :class="{
                            border: treeParams.border === true,
                            show: item.show,
                            last: item.lastRank,
                            showchild: item.showChild
                        }"
                    @click.stop="treeItemTap(item, index)"
                >
                    <image class="mix-tree-icon" :src="item.lastRank ? treeParams.lastIcon : item.showChild ? treeParams.currentIcon : treeParams.defaultIcon"></image>
                    {{item.department_name}}
                    
                    <!-- 新增选中当前项的功能 -->
                    <!-- <view @click.stop="currentItemTap(item, index)">{{item.department_name}}</view> -->
                    <view style="width: 60upx;text-align: center;margin-left: 5upx;" @click.stop="currentItemTap(item, index)">
                        <image class="mix-tree-icon" src="/static/mix-tree/currentNote.png"></image>
                    </view>     
                    
                </view>
            </block>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            list: {
                type: Array,
                default(){
                    return [];
                }
            },
            params: {
                type: Object,
                default(){
                    return {}
                }
            }
        },
        data() {
            return {
                treeList: [],
                treeParams: {
                    defaultIcon: '/static/mix-tree/defaultIcon.png',
                    currentIcon: '/static/mix-tree/currentIcon.png',
                    lastIcon: '',
                    border: false
                }
            }
        },
        watch: {
            list(list){
                this.treeParams = Object.assign(this.treeParams, this.params);
                // console.log(this.treeParams, this.params);
                this.renderTreeList(list);
            }
        },
        onLoad() {  
            console.log("mix-tree-dep onLoad")
        }, 
        methods: {
            //扁平化树结构
            renderTreeList(list=[], rank=0, parentId=[]){
                //liy:若无该if代码块的判断与处理,网络数据每刷新一次一级目录就会重复一次
                if(rank == 0) {
                    this.treeList = [];
                }
                list.forEach(item=>{
                    this.treeList.push({
                        department_id: item.department_id,
                        department_name: item.department_name,
                        parentId,  // 父级id数组
                        rank,  // 层级
                        showChild: false,  //子级是否显示
                        show: rank === 0  // 自身是否显示
                    })
                    if(Array.isArray(item.children) && item.children.length > 0){
                        let parents = [...parentId];
                        parents.push(item.department_id);
                        this.renderTreeList(item.children, rank+1, parents);
                    }else{
                        this.treeList[this.treeList.length-1].lastRank = true;
                    }
                })
            },
            // 点击
            treeItemTap(item){
                let list = this.treeList;
                let id = item.department_id;
                if(item.lastRank === true){
                    //点击最后一级时触发事件
                    this.$emit('treeItemClick', item);
                    return;
                }
                item.showChild = !item.showChild;
                list.forEach(childItem=>{
                    if(item.showChild === false){
                        //隐藏所有子级
                        if(!childItem.parentId.includes(id)){
                            return;
                        }
                        if(childItem.lastRank !== true){
                            childItem.showChild = false;
                        }
                        childItem.show = false;
                    }else{
                        if(childItem.parentId[childItem.parentId.length-1] === id){
                            childItem.show = true;
                        }
                    }
                })
            },
            // 实现选中中间item项
            currentItemTap(item){
                let list = this.treeList;
                let id = item.department_id;
                console.log("currentItemTap:" + id);
                this.$emit('treeItemClick', item);
                return;
            }
        }
    }
</script>

<style>
    .mix-tree-list{
        display: flex;
        flex-direction: column;
        padding-left: 30upx;
    }
    .mix-tree-item{
        display: flex;
        align-items: center;
        font-size: 30upx;
        color: #333;
        height: 0;
        opacity: 0;
        transition: .2s;
        position: relative;
    }
    .mix-tree-item.border{
        border-bottom: 1px solid #eee;
    }
    .mix-tree-item.show{
        height: 80upx;
        opacity: 1;
    }
    .mix-tree-icon{
        width: 26upx;
        height: 26upx;
        margin-right: 8upx;
        opacity: .9;
    }
    
    .mix-tree-item.showchild:before{
        transform: rotate(90deg);
    }
    .mix-tree-item.last:before{
        opacity: 0;
    }
</style>

主要新增代码为:

<!-- <view @click.stop="currentItemTap(item, index)">{{item.department_name}}</view> -->
<view style="width: 60upx;text-align: center;margin-left: 5upx;" @click.stop="currentItemTap(item, index)">
    <image class="mix-tree-icon" src="/static/mix-tree/currentNote.png"></image>
</view> 
currentItemTap(item){
    let list = this.treeList;
    let id = item.department_id;
    console.log("currentItemTap:" + id);
    this.$emit('treeItemClick', item);
    return;
}

2.2 功能拓展 — 实现选中中间项实现默认列表子项全展开

<template>
    <view class="content">
        <view class="mix-tree-list">
            <block v-for="(item, index) in treeList" :key="index">
                <view 
                    class="mix-tree-item"
                    :style="[{
                            paddingLeft: item.rank*15 + 'px',
                            zIndex: item.rank*-1 +50
                        }]"
                    :class="{
                            border: treeParams.border === true,
                            show: item.show,
                            last: item.lastRank,
                            showchild: item.showChild
                        }"
                    @click.stop="treeItemTap(item, index)"
                >
                    <image class="mix-tree-icon" :src="item.lastRank ? treeParams.lastIcon : item.showChild ? treeParams.currentIcon : treeParams.defaultIcon"></image>
                    {{item.department_name}}
                    
                    <!-- 新增选中当前项的功能 -->
                    <!-- <view @click.stop="currentItemTap(item, index)">{{item.department_name}}</view> -->
                    <view style="width: 60upx;text-align: center;margin-left: 5upx;" @click.stop="currentItemTap(item, index)">
                        <image class="mix-tree-icon" src="/static/mix-tree/currentNote.png"></image>
                    </view>     
                    
                </view>
            </block>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            list: {
                type: Array,
                default(){
                    return [];
                }
            },
            params: {
                type: Object,
                default(){
                    return {}
                }
            }
        },
        data() {
            return {
                treeList: [],
                treeParams: {
                    defaultIcon: '/static/mix-tree/defaultIcon.png',
                    currentIcon: '/static/mix-tree/currentIcon.png',
                    lastIcon: '',
                    border: false
                }
            }
        },
        watch: {
            list(list){
                this.treeParams = Object.assign(this.treeParams, this.params);
                // console.log(this.treeParams, this.params);
                this.renderTreeList(list);
            }
        },
        onLoad() {  
            console.log("mix-tree-dep onLoad")
        }, 
        methods: {
            //扁平化树结构
            renderTreeList(list=[], rank=0, parentId=[]){
                //liy:若无该if代码块的判断与处理,网络数据每刷新一次一级目录就会重复一次
                if(rank == 0) {
                    this.treeList = [];
                }
                list.forEach(item=>{
                    this.treeList.push({
                        department_id: item.department_id,
                        department_name: item.department_name,
                        parentId,  // 父级id数组
                        rank,  // 层级
                        showChild: false,  //子级是否显示(liy:默认所有子级都不显示)
                        show: rank === 0  // 自身是否显示
                    })
                    if(Array.isArray(item.children) && item.children.length > 0){
                        let parents = [...parentId];
                        parents.push(item.department_id);
                        this.renderTreeList(item.children, rank+1, parents);
                    }else{
                        this.treeList[this.treeList.length-1].lastRank = true;
                    }
                });
    
                //liy:实现默认展开所有子级
                this.treeList.forEach(childItem=>{
                    childItem.show = true;
                    //解决第一次点击条目不会收缩子项的问题(默认所有子级都不显示,所有这里要重新赋值),或者showChild默认值该为true即可
                    if(childItem.rank != 0){
                        this.treeList[childItem.rank -1].showChild = true;
                    }
                });

            },
            // 点击
            treeItemTap(item){
                let list = this.treeList;
                let id = item.department_id;
                if(item.lastRank === true){
                    //点击最后一级时触发事件
                    this.$emit('treeItemClick', item);
                    return;
                }
                item.showChild = !item.showChild;
                list.forEach(childItem=>{
                    if(item.showChild === false){
                        //隐藏所有子级
                        if(!childItem.parentId.includes(id)){
                            return;
                        }
                        if(childItem.lastRank !== true){
                            childItem.showChild = false;
                        }
                        childItem.show = false;
                    }else{
                        if(childItem.parentId[childItem.parentId.length-1] === id){
                            childItem.show = true;
                        }
                    }
                })
            },
            // 实现选中中间item项
            currentItemTap(item){
                let list = this.treeList;
                let id = item.department_id;
                console.log("currentItemTap:" + id);
                this.$emit('treeItemClick', item);
                return;
            }
        }
    }
</script>

<style>
    .mix-tree-list{
        display: flex;
        flex-direction: column;
        padding-left: 30upx;
    }
    .mix-tree-item{
        display: flex;
        align-items: center;
        font-size: 30upx;
        color: #333;
        height: 0;
        opacity: 0;
        transition: .2s;
        position: relative;
    }
    .mix-tree-item.border{
        border-bottom: 1px solid #eee;
    }
    .mix-tree-item.show{
        height: 80upx;
        opacity: 1;
    }
    .mix-tree-icon{
        width: 26upx;
        height: 26upx;
        margin-right: 8upx;
        opacity: .9;
    }
    
    .mix-tree-item.showchild:before{
        transform: rotate(90deg);
    }
    .mix-tree-item.last:before{
        opacity: 0;
    }
</style>

  • 主要新增代码为:
//liy:实现默认展开所有子级
this.treeList.forEach(childItem=>{
    childItem.show = true;
});
默认展开所有子级.png

问题:因为代表子级是否显示的标志位showChild默认为false(showChild: false),即默认所有子级都不显示;所以如果仅实现展开每个子级而不重新设置其父级的showChild标志位,那么第一次点击条目则不会收缩子项。
解决方案1:还需将标志位showChild默认设置为true(showChild: true
解决方案2

//liy:实现默认展开所有子级
this.treeList.forEach(childItem=>{
    childItem.show = true;
    //解决第一次点击条目不会收缩子项的问题(默认所有子级都不显示,所有这里要重新赋值),或者showChild默认值该为true即可
    if(childItem.rank != 0){
        this.treeList[childItem.rank -1].showChild = true;
    }
});

2.3 功能拓展 — 实现选中中间项实现默认仅展开第二级

2.2 功能拓展 — 实现默认列表子项全展开 的基础上修改代码:

//liy:实现默认展开所有子级
/**this.treeList.forEach(childItem=>{
    childItem.show = true;
    //解决第一次点击条目不会收缩子项的问题(默认所有子级都不显示,所有这里要重新赋值),或者showChild默认值该为true即可
    if(childItem.rank != 0){
        this.treeList[childItem.rank -1].showChild = true;
    }
});**/

//liy:实现默认仅展开第二级
this.treeList.forEach(childItem=>{
    if(childItem.rank == 1){//层级从0开始,1代表第二级
        childItem.show = true;//展开第二级
        this.treeList[0].showChild = true;//解决第一次点击条目不会收缩子项的问题(默认所有子级都不显示,所有这里要重新赋值)
    }   
});
默认仅展开第二级.png

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

推荐阅读更多精彩内容