《ElementUI 基础知识》el-tree 之“我的电脑”目录结构效果

前言

项目需求,Web 端获取服务器文件夹目录结构。目录数据是调接口获取,本篇略过,直接展现数据!

效果

实现

html

  • 代码 8 - 15 行,自定义节点信息;
  • 代码 9 - 14 行,判断 icon 显示;
html 复制代码
<el-tree
    ref="tree"
    :data="treeData"
    accordion
    node-key="level"
    :props="defaultTreeProps"
    @node-click="handleNodeClick">
    <span slot-scope="{ node, data }"  style="display: flex;">
        <!-- 父节点-未展开 -->
        <i v-if="node.childNodes.length > 0 && !node.expanded" class="my-icon folder-icon"/>
        <!-- 父节点-已展开 -->
        <i v-else-if="node.childNodes.length > 0 && node.expanded" class="my-icon folder-open-icon"/>
        <!-- 子节点 -->
        <i v-else class="my-icon file-icon"/>
        {{ node.label}}
    </span>
</el-tree>

数据

js 复制代码
treeData: [{
    text: '我的电脑',
    children: [
        {
            text: '桌面:\\',
            path: "C:\\Users\\admin\\Desktop",
            children: [
                {
                    text: '新建文件夹',
                    path: "C:\\Users\\admin\\Desktop\\新建文件夹",
                    children: [
                        {
                            text: 'test.txt',
                            path: "C:\\Users\\admin\\Desktop\\test.txt",
                        },
                        {
                            text: '报表.xlsx',
                            path: "C:\\Users\\admin\\Desktop\\报表.xlsx",
                        },
                    ]
                },
                {
                    text: '测试文档.xlsx',
                    path: "C:\\Users\\admin\\Desktop\\测试文档.xlsx",
                },
            ]

        },
        {
            text: 'C:\\',
            path: "C:\\",
            children: [
                {
                    text: 'Program Files',
                    path: "C:\\Users\\admin\\Desktop\\Program Files",
                    children: [
                        {
                            text: 'Microsoft Office',
                            path: "C:\\Users\\admin\\Desktop\\Microsoft Office",
                            children: [
                                {
                                    text: '季度总结.docx',
                                    path: "C:\\Users\\admin\\Desktop\\Microsoft Office\\季度总结.docx",
                                },
                                {
                                    text: '季度汇报.pptx',
                                    path: "C:\\Users\\admin\\Desktop\\Microsoft Office\\季度汇报.pptx",
                                },
                            ]
                        },
                        {
                            text: '报表.xlsx',
                            path: "C:\\Users\\admin\\Desktop\\报表.xlsx",
                        },
                    ]
                },
                {
                    text: '测试文档.xlsx',
                    path: "C:\\Users\\admin\\Desktop\\测试文档.xlsx",
                    leaf: true,
                },
            ]

        },
        {
            text: 'D:\\',
            path: "D:\\",
            children: [
                {
                    text: '游戏',
                    path: "D:\\游戏",
                    children: [
                        {
                            text: '游戏01.xlsx',
                            path: "D:\\游戏\\游戏01.xlsx",
                        },
                        {
                            text: '游戏02.xlsx',
                            path: "D:\\游戏\\游戏02.xlsx",
                        },
                    ]
                },
            ]

        },
        {
            text: 'E:\\',
            path: "E:\\",
            children: [
                {
                    text: '电影',
                    path: "E:\\电影",
                    children: [
                        {
                            text: '玩具总动员.mp4',
                            path: "E:\\电影\\玩具总动员.mp4",
                        },
                        {
                            text: '冰雪奇缘.mp4',
                            path: "E:\\电影\\冰雪奇缘.mp4",
                        },
                        {
                            text: '疯狂动物城.mp4',
                            path: "E:\\电影\\疯狂动物城.mp4",
                        },
                    ]
                },
            ]
        },
        
    ]
}],

完整代码

注意是 Vue2 文件

html 复制代码
<template>
    <div class="dir-tree-main">
        <el-dialog title="我的电脑" :visible.sync="dialogVisible" class="directory-dialog" v-if="dialogVisible">
            <el-tree
                ref="tree"
                :data="treeData"
                accordion
                node-key="level"
                :props="defaultTreeProps"
                @node-click="handleNodeClick">
                <span slot-scope="{ node, data }"  style="display: flex;">
                    <!-- 父节点-未展开 -->
                    <i v-if="node.childNodes.length > 0 && !node.expanded" class="my-icon folder-icon"/>
                    <!-- 父节点-已展开 -->
                    <i v-else-if="node.childNodes.length > 0 && node.expanded" class="my-icon folder-open-icon"/>
                    <!-- 子节点 -->
                    <i v-else class="my-icon file-icon"/>
                    {{ node.label}}
                </span>
            </el-tree>
            <div class="btns">
                <el-button type="info" @click="dialogVisible=false;">取消</el-button>
                <el-button type="primary" @click="onSelect">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            dialogVisible: false,
            treeData: [{
                text: '我的电脑',
                children: [
                    {
                        text: '桌面:\\',
                        path: "C:\\Users\\admin\\Desktop",
                        children: [
                            {
                                text: '新建文件夹',
                                path: "C:\\Users\\admin\\Desktop\\新建文件夹",
                                children: [
                                    {
                                        text: 'test.txt',
                                        path: "C:\\Users\\admin\\Desktop\\test.txt",
                                    },
                                    {
                                        text: '报表.xlsx',
                                        path: "C:\\Users\\admin\\Desktop\\报表.xlsx",
                                    },
                                ]
                            },
                            {
                                text: '测试文档.xlsx',
                                path: "C:\\Users\\admin\\Desktop\\测试文档.xlsx",
                            },
                        ]

                    },
                    {
                        text: 'C:\\',
                        path: "C:\\",
                        children: [
                            {
                                text: 'Program Files',
                                path: "C:\\Users\\admin\\Desktop\\Program Files",
                                children: [
                                    {
                                        text: 'Microsoft Office',
                                        path: "C:\\Users\\admin\\Desktop\\Microsoft Office",
                                        children: [
                                            {
                                                text: '季度总结.docx',
                                                path: "C:\\Users\\admin\\Desktop\\Microsoft Office\\季度总结.docx",
                                            },
                                            {
                                                text: '季度汇报.pptx',
                                                path: "C:\\Users\\admin\\Desktop\\Microsoft Office\\季度汇报.pptx",
                                            },
                                        ]
                                    },
                                    {
                                        text: '报表.xlsx',
                                        path: "C:\\Users\\admin\\Desktop\\报表.xlsx",
                                    },
                                ]
                            },
                            {
                                text: '测试文档.xlsx',
                                path: "C:\\Users\\admin\\Desktop\\测试文档.xlsx",
                                leaf: true,
                            },
                        ]

                    },
                    {
                        text: 'D:\\',
                        path: "D:\\",
                        children: [
                            {
                                text: '游戏',
                                path: "D:\\游戏",
                                children: [
                                    {
                                        text: '游戏01.xlsx',
                                        path: "D:\\游戏\\游戏01.xlsx",
                                    },
                                    {
                                        text: '游戏02.xlsx',
                                        path: "D:\\游戏\\游戏02.xlsx",
                                    },
                                ]
                            },
                        ]

                    },
                    {
                        text: 'E:\\',
                        path: "E:\\",
                        children: [
                            {
                                text: '电影',
                                path: "E:\\电影",
                                children: [
                                    {
                                        text: '玩具总动员.mp4',
                                        path: "E:\\电影\\玩具总动员.mp4",
                                    },
                                    {
                                        text: '冰雪奇缘.mp4',
                                        path: "E:\\电影\\冰雪奇缘.mp4",
                                    },
                                    {
                                        text: '疯狂动物城.mp4',
                                        path: "E:\\电影\\疯狂动物城.mp4",
                                    },
                                ]
                            },
                        ]
                    },
                    
                ]
            }],
            defaultTreeProps: {
                children: 'children',
                label: 'text'
            },
            currNode: null
        }
    },
    methods: {
        // node click
        handleNodeClick(data, node, component) {
            console.log(data, node);
            // 当前node
            this.currNode = node;
        },
        // 确定
        onSelect() {
            // TODO
            this.dialogVisible = false;
        }
    },
    mounted() {
        setTimeout(() => {
            this.dialogVisible = true;
        }, 100)
    },
}
</script>
<style lang="stylus" scoped>
.dir-tree-main {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: #999;
}
.directory-dialog {
    .el-dialog {
        margin-top: 10vh !important;
    }
    .el-tree {
        height: 50vh;
        overflow-y: scroll;
        border: 1px solid #999;
    }
    .btns {
        margin-top: 20px;
        display: flex;  
    }
}
.my-icon {
    display: inline-block;
    width: 16px;
    height: 16px;
    background-size: 16px 16px;
    margin-right: 8px;
}
.file-icon {
    background: url(../assets/images/file2.png);
}
.folder-icon {
    background: url(../assets/images/folder.png);
    width: 15px;
    height: 15px;
    background-size: 15px 15px;
}
.folder-open-icon {
    background: url(../assets/images/folder-open2.png);
}
</style>
相关推荐
Martin -Tang6 分钟前
vite和webpack的区别
前端·webpack·node.js·vite
迷途小码农零零发7 分钟前
解锁微前端的优秀库
前端
王解1 小时前
webpack loader全解析,从入门到精通(10)
前端·webpack·node.js
老码沉思录1 小时前
写给初学者的React Native 全栈开发实战班
javascript·react native·react.js
我不当帕鲁谁当帕鲁1 小时前
arcgis for js实现FeatureLayer图层弹窗展示所有field字段
前端·javascript·arcgis
那一抹阳光多灿烂1 小时前
工程化实战内功修炼测试题
前端·javascript
放逐者-保持本心,方可放逐2 小时前
微信小程序=》基础=》常见问题=》性能总结
前端·微信小程序·小程序·前端框架
毋若成4 小时前
前端三大组件之CSS,三大选择器,游戏网页仿写
前端·css
红中马喽4 小时前
JS学习日记(webAPI—DOM)
开发语言·前端·javascript·笔记·vscode·学习
Black蜡笔小新5 小时前
网页直播/点播播放器EasyPlayer.js播放器OffscreenCanvas这个特性是否需要特殊的环境和硬件支持
前端·javascript·html