Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

我要设置elementui 的el-tree的层级只显示2个层级怎样动态设置层级呢?

如题:
我要设置elementui 的el-tree的层级只显示2个层级怎样动态设置层级呢?

如图:只需要显示2个层级。但是每个层级都是单独的接口。
https://element.eleme.cn/#/zh...
里面说的:分别通过default-expanded-keysdefault-checked-keys设置默认展开和默认选中的节点。需要注意的是,此时必须设置node-key,其值为节点数据中的一个字段名,该字段在整棵树中是唯一的。image.png
image.png

image.png

image.png

如果设置node-key为id的话。我该怎么去动态获取呢?数组的长度也是动态的。是不是可以把:default-expanded-keys绑定一个动态数组传进去参数吗?
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

已解决:
` <div class="head-container">

      <el-tree
        :data="deptOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        ref="tree"
        node-key="id"
        :default-expanded-keys="nodeKey"
        @node-click="handleNodeClick"
      />
    </div>`
    
    在data里面设置一个nodeKey 把第一级的id赋值给nodeKey注意nodeKey必须是数组即可:
     this.nodeKey.push(this.deptOptions[0].id)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...