天天看點

elementui樹元件el-tree預設選中展開某些節點

問題1:左側圖示不想要,換個别的

問題2:通過接口擷取樹,點選選中某條資料,之後通過接口更新樹資料,沒有保留選中狀态,希望保留

對于問題1:

把想要的圖示做成精靈圖,然後通過background去設定,具體代碼如下:

1     .report_projectSub /deep/ .el-tree .el-icon-caret-right:before {
 2       content: "";
 3       display: inline-block;
 4       font-size: 18px;
 5       height:14px;
 6       text-align: center;
 7       width: 32px;
 8       line-height: 14px;
 9       padding-top:-8px;
10       background:url(./treeActive.png) no-repeat ;//設定背景,再調整背景位置大小
11       background-size: 100px;
12       background-position: -14px -11px;
13     }
14     .report_projectSub /deep/ .el-tree .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
15       content: "";
16       display: inline-block;
17       text-align: center;
18       font-size: 18px;
19       width: 32px;
20       height:14px;
21       text-align: center;
22       line-height: 14px;
23       background:url(./treeActive.png) no-repeat;//設定背景,再調整背景位置大小
24       background-size: 105px;
25       background-position: -65px -12px;
26     }
27     .report_projectSub /deep/ .el-tree .is-leaf:before{
28       background:none;//去掉最後一層的背景
29     }      
elementui樹元件el-tree預設選中展開某些節點

設定前

elementui樹元件el-tree預設選中展開某些節點

設定後

<el-tree
     ref="treeSub"//設定ref
     :data="treeData"
     :props="treedefaultProps"
     node-key="id"
     :highlight-current="true"
     :default-expanded-keys="defaultshowKeys"//設定預設選中的屬性
     accordion
     @node-click="treeClick"//選擇之後,用defaultshowKeys記住現在的選擇>      

  this.defaultshowKeys = [this.rightShow.typeId];

this.$nextTick(()=>{
     this.$refs.treeSub.setCurrentKey(this.rightShow.typeId)//通過設定的ref設定setCurrentKey去渲染tree的預設選中
  })