el-tree:如何實現(xiàn)自定義節(jié)點或者整體背景顏色自定義

1、自定義節(jié)點(字體顏色、圖標(biāo)等)

image.png

官網(wǎng)提供了render-content和 scoped slot兩種方法可對樹節(jié)點內(nèi)容自定義,使用render-content指定渲染函數(shù),該函數(shù)返回需要的節(jié)點區(qū)內(nèi)容即可。渲染函數(shù)的用法請參考 Vue 文檔。使用 scoped slot 會傳入兩個參數(shù)node和data,分別表示當(dāng)前節(jié)點的 Node 對象和當(dāng)前節(jié)點的數(shù)據(jù)。以下的代碼則采用的 scoped slot方法。

                <el-tree
                        class="filter-tree"
                        ref="jportalFuncTreeRef"
                        :data="orgFuncTreeData"
                        node-key="id"
                        highlight-current
                        :props="{
                            children: 'children',
                            label: 'indexName',
                        }"
                        accordion
                        :expand-on-click-node="true"
                        :check-on-click-node="false"
                        :default-checked-keys="[]"
                        @node-click="treeNodeClick"
                        :filter-node-method="treeNodeFilter"
                        :empty-text="'無數(shù)據(jù)!'">
                          <span slot-scope="{ node, data }" class="slot-tree-node">
                            <span :class="data.category == 2 ? 'js-label-menu js-label' : 'js-label'">{{ node.label }}</span>
                            <span class="js-op-list">
                                <span class="js-op-item" @click.stop="funcTreeNodeDown(node, data)" title="下移">
                                    <i class="el-icon-arrow-down primary"></i>
                                </span>
                                <span class="js-op-item" @click.stop="funcTreeNodeUp(node, data)" title="上移">
                                    <i class="el-icon-arrow-up primary"></i>
                                </span>
                                   <span class="js-op-item" @click.stop="funcTreeDel(node, data)" title="刪除">
                                    <i class="el-icon-delete danger"></i>
                                </span>
                            </span>
                        </span>
                    </el-tree>

2、實現(xiàn)不同級別樹節(jié)點的背景顏色自定義

image.png

如果再采用自定義節(jié)點的方式來修改背景顏色,會出現(xiàn)下圖的問題,前面部分無法渲染顏色

image.png

分析過程:

1、首先console.log(this.$refs.tree),不難發(fā)現(xiàn),根本無法通過class來直接區(qū)分節(jié)點屬于哪個級別,但是treeItems的數(shù)據(jù)是有序的,可以根據(jù)index來區(qū)分。

image.png

2、所以,我們首先要做的第一步是將樹形結(jié)構(gòu)的數(shù)據(jù)轉(zhuǎn)換成list

/**
 * 樹轉(zhuǎn)list
 */
treeToList(tree) {
  for (var i in tree) {
    var node = tree[i];
    this.treeList.push({
      parentId: null,
      modelIndexId: node.modelIndexId,
      modelIndexName: node.modelIndexName,
      modelIndexCode: node.modelIndexCode,
      grade: node.grade,
      type: node.type,
      weight: node.weight
    })
    if (node.children && node.children.length !== 0) {
      this.toListDF(node.children, this.treeList, node.modelIndexId); // 遍歷子樹,并加入到list中.
    }
  }
  return this.treeList;
},


   /**
     * 深度優(yōu)先遍歷樹
     * 一個遞歸方法
     * @params tree:要轉(zhuǎn)換的樹結(jié)構(gòu)數(shù)據(jù)
     * @params list:保存結(jié)果的列表結(jié)構(gòu)數(shù)據(jù),初始傳list = []
     * @params parentId:當(dāng)前遍歷節(jié)點的父級節(jié)點id,初始為null(因為根節(jié)點無parentId)
     **/
toListDF(tree, list, parentId) {
  for (var i in tree) { // 遍歷最上層
    // 將當(dāng)前樹放入list中
    var node = tree[i];
    list.push({
      parentId: parentId,
      modelIndexId: node.modelIndexId,
      modelIndexName: node.modelIndexName,
      modelIndexCode: node.modelIndexCode,
      grade: node.grade,
      type: node.type,
      weight: node.weight
    });
    // 如果有子結(jié)點,再遍歷子結(jié)點
    if (node.children && node.children.length !== 0) {
      this.toListDF(node.children, list, node.modelIndexId) // 遞歸
    }
  }
},

3、然后,我們可以從下圖發(fā)現(xiàn),從這里可以控制style。

image.png

4、在掛載頁面時,等DOM加載完,遍歷list即可

 this.$nextTick(() => {
      treeList.find((item, index) => {
        // console.log(item.grade,item.modelIndexName,index)
        if (item.grade === 1) {
          this.$refs.tree.treeItems[index].style.backgroundColor = 'rgba(220, 229, 254, 1)'
        } else if (item.grade === 2) {
          this.$refs.tree.treeItems[index].style.backgroundColor = 'rgba(249, 249, 249, 1)'
        } else if (item.grade === 3) {
          this.$refs.tree.treeItems[index].style.backgroundColor = 'white'
        }
      })
    })

總結(jié):應(yīng)該有更好更合適的方法,本人不太推薦使用這種方法,有點麻煩,期待有人能分享交流一下其他的實現(xiàn)方式。

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

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

  • 寫在前面的話 代碼中的# > 表示的是輸出結(jié)果 輸入 使用input()函數(shù) 用法 注意input函數(shù)輸出的均是字...
    FlyingLittlePG閱讀 3,208評論 0 9
  • feisky云計算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,332評論 0 5
  • 程序設(shè)計中常使用樹型結(jié)構(gòu)來表征某些數(shù)據(jù)的關(guān)聯(lián)關(guān)系,如上下級、欄目結(jié)構(gòu)、商品分類、菜單、回復(fù)等。 分類的層級關(guān)系可以...
    JunChow520閱讀 4,175評論 4 3
  • 憎惡這樣的自己
    尼勾拉斯泰勒艾閱讀 165評論 0 0
  • 前幾天子棟老師分享,家里的女人要灑掃庭除,我想了想自己很少做,因為和婆婆住一起,幾乎所有的事情,婆婆都做了,所以我...
    林玉珍閱讀 329評論 2 4

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