el-tree 根据id滚动到指定位置方法
时间: 2023-10-08 11:11:55 浏览: 48
el-tree 提供了scrollIntoView方法,可以实现根据id滚动到指定位置。
示例代码:
```html
<template>
<el-tree :data="data" ref="tree"></el-tree>
<el-button @click="scrollToNode">滚动到指定节点</el-button>
</template>
<script>
export default {
data() {
return {
data: [
{
id: 1,
label: '一级 1',
children: [
{
id: 2,
label: '二级 1-1'
},
{
id: 3,
label: '二级 1-2'
}
]
},
{
id: 4,
label: '一级 2',
children: [
{
id: 5,
label: '二级 2-1'
},
{
id: 6,
label: '二级 2-2'
}
]
}
]
};
},
methods: {
scrollToNode() {
const node = this.$refs.tree.getNode(3); // 获取id为3的节点
node && node.scrollIntoView(); // 滚动到指定节点
}
}
};
</script>
```
在上面的示例中,点击“滚动到指定节点”按钮会滚动到id为3的节点,也就是“二级 1-2”节点。