项目场景:使用el-tree组件,搜索后动态设置数据全部展示
问题描述
el-tree动态设置是否全部展开属性,值已经改变,但是没有展开
c
<el-select
v-model="TableName"
placeholder="请选择"
>
<el-input placeholder="请输入关键词" v-model="inputValue" maxlength="40">
<i slot="suffix" class="el-input__icon el-icon-search" @click="getTreeData"></i>
</el-input>
<el-option :value="TableName" style="height: 200px; overflow-y: auto">
<el-tree
v-loading="loading"
:data="treeData"
:index="index"
:props="defaultProps"
:default-expand-all="expandAll"
node-key="puid"
highlight-current
@node-click="
(data, node, item) =>
handleNodeClick(data, node, item, index, 'tableName')
"
/>
</el-option>
</el-select>
expandAll动态设置值为true false,值已经改变,树节点却没有展开
原因分析:
default-expand-all属性在第一次渲染时才生效,动态改变后,树早已渲染出来,所以需要等数据出来后重新渲染
解决方案:
css
加个 v-if="refreshTree"
<el-tree ref="tree"
v-loading="loading"
:data="treeData"
:index="index"
:props="defaultProps"
:default-expand-all="expandAll"
node-key="puid"
highlight-current
v-if="refreshTree"
@node-click="
(data, node, item) =>
handleNodeClick(data, node, item, index, 'tableName')
"
/>
父组件请求接口后处理
this.$refs.tree.refreshTree = false;
this.$refs.tree.expandAll = inputValue ? true : false;//这里我是根据输入框是否有值判断是否是搜索后,大家可以酌情处理
this.$refs.tree.$nextTick(() => {
this.$refs.tree.refreshTree = true;
});