el-tree添加虛線指示和圖標(biāo)

image.png

1、添加虛線代碼

為el-tree添加這兩個(gè)屬性

class="tree-line"
:indent="0"

完整代碼

<el-tree
    class="tree-line"
    :indent="0"
    :data="treeOption"
    :props="defaultProps"
    @node-click="getCurrentNode"
>

style

// 記得去掉scoped
<style lang="scss">
    .tree-line {
        .el-tree-node {
            position: relative;
            padding-left: 16px; // 縮進(jìn)量
        }
        .el-tree-node__children {
            padding-left: 16px; // 縮進(jìn)量
        }

        // 豎線
        .el-tree-node::before {
            content: '';
            height: 100%;
            width: 1px;
            position: absolute;
            left: -3px;
            top: -26px;
            border-width: 1px;
            border-left: 1px dashed #52627c;
        }
        // 當(dāng)前層最后一個(gè)節(jié)點(diǎn)的豎線高度固定
        .el-tree-node:last-child::before {
            height: 38px; // 可以自己調(diào)節(jié)到合適數(shù)值
        }

        // 橫線
        .el-tree-node::after {
            content: '';
            width: 24px;
            height: 20px;
            position: absolute;
            left: -3px;
            top: 12px;
            border-width: 1px;
            border-top: 1px dashed #52627c;
        }

        // 去掉最頂層的虛線,放最下面樣式才不會(huì)被上面的覆蓋了
        & > .el-tree-node::after {
            border-top: none;
        }
        & > .el-tree-node::before {
            border-left: none;
        }

        // 展開關(guān)閉的icon
        .el-tree-node__expand-icon {
            font-size: 16px;
            // 葉子節(jié)點(diǎn)(無(wú)子節(jié)點(diǎn))
            &.is-leaf {
                color: transparent;
                // display: none; // 也可以去掉
            }
        }
    }
}

2、添加圖標(biāo)代碼

圖標(biāo)可以是elementUi的icon,也可以直接引入iconfont的圖標(biāo)

                      <el-tree
                            class="tree-line"
                            :indent="0"
                            :data="treeOption"
                            :props="defaultProps"
                            @node-click="getCurrentNode"
                        >
                            <span
                                class="custom-tree-node"
                                slot-scope="{ node, data }"
                            >
                                <!-- parentId == 0說(shuō)明為父節(jié)點(diǎn),圖標(biāo)為 icon-weizhidingwei-->
                                <span
                                    v-if="data.parentId == 0"
                                    class="icon-weizhidingwei"
                                >
                                </span>
                                <!-- 否則為子節(jié)點(diǎn) ,圖標(biāo)為 icon-weizhi-->
                                <span
                                    v-else
                                    class="icon-weizhi"
                                    style="margin-right: 3px"
                                ></span>
                                <span>{{ node.label }}</span>
                            </span>
                        </el-tree>

除此之外還可以在treeData中配置icon屬性,屬性值為icon的名稱,渲染不同圖標(biāo)
html如下

               <span class="custom-tree-node" slot-scope="{ node, data }">
                    <span>
                        <i :class="data.icon">{{ node.label }}</i>
                    </span>              
                </span>

treeData如下

            treeData: [{
                    id: 1,
                    label: '一級(jí) 1',
                    icon:'el-icon-success',
                    children: [{
                        id: 4,
                        label: '二級(jí) 1-1',
                        children: [{
                            id: 9,
                            label: '三級(jí) 1-1-1',
                            icon: 'el-icon-info'
                        }, {
                            id: 10,
                            label: '三級(jí) 1-1-2'
                        }]
                    }]
                }, {
                    id: 2,
                    label: '一級(jí) 2',
                    children: [{
                        id: 5,
                        label: '二級(jí) 2-1',
                    }, {
                        id: 6,
                        label: '二級(jí) 2-2'
                    }]
                }, {
                    id: 3,
                    label: '一級(jí) 3',
                    children: [{
                        id: 7,
                        label: '二級(jí) 3-1'
                    }, {
                        id: 8,
                        label: '二級(jí) 3-2'
                    }]
                }]

圖標(biāo)樣式微調(diào)

    .icon-weizhidingwei:before {
        margin-right: 10px;
        color: #409eff;
    }
    .icon-weizhi:before {
        margin-right: 10px;
        color: #409eff;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容