<el-tree
:empty-text="emptyText"
id="filterTree"
class="filter-tree"
style="overflow:scroll; overflow-x: hidden;flex:1;"
:style="{ height: tHeight - 60 + 'px' }"
node-key="id"
:data="orgTreeData"
:props="defaultProps"
@node-click="getNodeData"
default-expand-all
highlight-current
ref="orgTree"
:filter-node-method="filterNode"
>
</el-tree>
getTreeData() {
this.emptyText = '數據加載中。。。';
if (this.orgTreeData.length === 0) {
projectDataAPI.listOrgTree().then(response => {
this.orgTreeData = response.data;
if (this.orgTreeData.length === 0) {
this.emptyText = '暫無數據';
}
/**
*下面的代碼是核心代碼 滾動到可見范圍區 獲取樹的數據后讓指定節點高亮 獲取高亮節點距離父元素的頂部距離(offsetTop) 獲得父容器的文檔內容的高度(scrollHeight) 父容器的高度(clientHeight)
*當offsetTop > clientHeight 將滾動條滾動到可見區域
*/
this.$nextTick(async () => {
await this.$refs.orgTree.setCurrentKey(this.projectId);
let nodeOffsetTop = document.querySelector(
'.filter-tree .is-current'
).offsetTop;
let parentNodeClientHeight = document.querySelector('#filterTree')
.clientHeight;
let parentNodeScrollHeight = document.querySelector(
'.filter-tree .el-tree-node'
).scrollHeight;
if (nodeOffsetTop > parentNodeClientHeight) {
document.querySelector('#filterTree').scrollTop =
nodeOffsetTop - parentNodeClientHeight / 2;
}
});
});
}
},