之所以使用懒加载是为了提高性能,而且只有在懒加载模式下默认会给所有显示节点设置展开按钮。leaf也可以做到,但是要操作数据比较麻烦。

要实现懒加载模式下的模糊查询以及重新加载必须要使用data与lazy、load相结合。

lazy和load负责树的初始加载和懒加载,还要绑定node-expand事件加载子节点数据。

data负责模糊查询下填充查询结果树,它与懒加载结合使用并不冲突。

关键code一:

            this.$nextTick(() => {
let nodedata = this.node.childNodes[0]
nodedata.expanded = true
nodedata.loadData()
})

作用:在初次设置顶级菜单并想展开二级菜单时候必须用到,在根节点渲染上去后执行展开并加载二级节点。

关键code二:

    /**
* @param item 搜索输入内容
*/
treeChange(item) {
//存储 搜索输入的值
if (item || item === 0) {
let params = { organizationId: item.organizationId, dimon: this.dimensionModel }
changeTreeData(params).then((res) => {
this.treeData = res.resultData.organizationVos
})
}
},

作用:在搜索控件值改变后触发,查询结果赋值给data达到重新加载tree的目的。

关键code三:

    /**
* 重载树
*/
reloadTree() {
this.node.childNodes = []
this.loadNode(this.node, this.resolveFunc)
}

作用:初始化Tree控件数据,node和resolveFunc两个参数需要在load绑定的事件中手动缓存起来方便之后调用。

最后附上源码给大家参考!!!

<template>
<div class="ns-biz-many-type-tree">
<div>
<el-select v-model="value" placeholder="请选择" size="small">
<el-option
v-for="item in treeTypeOptions"
:key="item.value"
:label="item.label"
:value="item.value"
@change="treeTypeChange"
>
</el-option>
</el-select>
<el-button type="primary" size="small">搜索</el-button>
</div> <el-autocomplete
v-model="treeSearchInput"
:fetch-suggestions="remoteSearch"
value-key="organizationName"
placeholder="请输入内容"
size="small"
@select="treeChange"
>
<i class="el-icon-search el-input__icon" slot="suffix" @click="handleIconClick"> </i
></el-autocomplete> <el-tree
ref="newTree"
:props="defaultProps"
:data="treeData"
lazy
:load="loadNode"
@check-change="handleCheckChange"
@node-expand="expandNode"
>
</el-tree>
</div>
</template>
<script>
import {
getOrgTreeData,
getOrgTreeChildData,
getSearchData,
changeTreeData,
} from './service/NS-biz-organize-tree'
export default {
name: 'Ns-biz-many-type-tree',
props: {},
data() {
return {
treeData: [],
treeTypeOptions: [
{
value: '1',
label: '组织架构',
labelName: 'organizationName',
childrenName: 'childOrganizations',
},
{
value: '2',
label: '工作组',
labelName: 'groupName',
childrenName: 'childGroup',
},
],
defaultProps: {
label: 'organizationName',
children: 'childOrganizations',
},
node: {},
treeSearchInput: '',
value: '1',
topMenuName: '',
dimensionModel: 'adm',
resolveFunc: function () {},
}
},
mounted() { },
methods: {
// 树节点点击事件
handleCheckChange() {},
// 树类型切换事件
treeTypeChange() {},
//快捷图标搜索
handleIconClick() {},
querySearchAsync() {},
// 加载Tree节点
loadNode(node, resolve) {
console.log('树对象', node)
if (node.level === 0) {
this.node = node
this.resolveFunc = resolve
// 组织架构树
if (this.value == '1') {
getOrgTreeData({ dimon: this.dimensionModel, selectAll: false }).then((res) => {
let { organizationVos } = res.resultData
this.$nextTick(() => {
let nodedata = this.node.childNodes[0]
nodedata.expanded = true
nodedata.loadData()
})
return resolve([
{
organizationName: organizationVos[0].organizationName,
organizationId: organizationVos[0].organizationId,
},
])
})
} else {
}
} else {
setTimeout(() => {
getOrgTreeChildData({
id: node.data.organizationId,
dimon: this.dimensionModel,
selectAll: false,
}).then((res) => {
resolve(res.resultData.organizationVos)
})
}, 500)
}
},
// 节点展开事件
expandNode(obj, node, self) {
if (node.level !== 0) {
console.log('node', node)
if (node.data.childOrganizations == null) {
if (this.value == '1') {
getOrgTreeChildData({
id: node.data.organizationId,
dimon: this.dimensionModel,
selectAll: false,
}).then((res) => {
node.data.childOrganizations = res.resultData.organizationVos
})
} else {
}
}
}
},
/**
* 树 输入触发 搜索查询
* @param query
* @param cb
*/
remoteSearch(query, cb) {
if (query !== '') {
let params = { organizationName: query, dimon: this.dimensionModel }
getSearchData(params)
.then((r) => {
cb(r.resultData)
})
.catch((err) => {
console.log(err)
})
} else {
cb([])
}
},
/**
* @param item 搜索输入内容
*/
treeChange(item) {
// 存储 搜索输入的值
if (item || item === 0) {
let params = { organizationId: item.organizationId, dimon: this.dimensionModel }
changeTreeData(params).then((res) => {
this.treeData = res.resultData.organizationVos
})
}
},
/**
* 重载树
*/
reloadTree() {
this.node.childNodes = []
this.loadNode(this.node, this.resolveFunc)
},
},
watch: {
value(val) {
this.reloadTree()
},
treeSearchInput(val) {
if (val == '') {
this.reloadTree()
}
},
},
}
</script>
<style lang="less" scoped> </style>

希望能够帮助大家,如果哪些地方有缺陷还请大家指正。

作者:一个想留在杭州发展的撸码青年。

ElementUI Tree控件在懒加载模式下的重新加载和模糊查询的更多相关文章

  1. element-ui tree控件获取当前节点和父节点

    今天使用element-ui 遇到两个问题,第一个问题是获取tree控件的当前节点和父节点, 一开始使用tree控件的getCurrentNode()函数,结果发现返回的是当前节点的data属性,和u ...

  2. vue+element项目中使用el-dialog弹出Tree控件报错问题

    1. 按正常的点击按钮,显示dialog弹出的Tree控件,然后把该条数据下的已经选中的checkbox , 用setCheckedNodes或者setCheckedKeys方法选择上 , 报下面这个 ...

  3. 基于vue开发的element-ui树形控件报错问题解决

    对没错,这次又是ElementUI的问题,在使用ElementUI中的 tree 树形控件时需要动态添加DOM元素,但是在使用文档中给出的案例的时候会报错. 案例:ElementUI树形控件 - 自定 ...

  4. easyui中tree控件添加自定义图标icon

    来源于:http://blog.163.com/lintianhuanhai@126/blog/static/165587366201421704420256/ <!DOCTYPE html&g ...

  5. DWZ (JUI) 教程 tree 控件的选中事件

    DWZ (JUI) 教程 tree 控件的选中事件 先简单说一下流程 第一步 当然是先定义好回调事件了 function checkCallback(json){ ........... ...... ...

  6. webdynpro tree控件使用

    1.  首先创建一个TREE控件 2.  在Tree下面创建一个TREE  NODE  TYPE ,node type 是可以继续展开的,而Item type是无法展开的. 3. 创建node.,下面 ...

  7. antd的Tree控件实现点击展开功能

    antd 的 Tree 控件没有提供点击展开的功能,只能通过左边的三角形实现展开和收起,没办法只好自己实现这个功能. 先看效果 如图实现的是类似 Mac 文件目录形式的结构,有箭头代表是个文件夹,点击 ...

  8. 使用elementui树形控件写项目小结

    使用tree pagination serch table 实现功能 项目难点主要解析后台传递的代码,线性转树形,这儿加上一个大神的解析 https://blog.csdn.net/dandanzmc ...

  9. ELementUI 树形控件tree 获取子节点同时获取半选择状态的父节点ID

    使用element-ui  tree树形控件的时候,在选择一个子节点后,使用getCheckedKeys 后,发现只能返回子节点的ID,但是其父节点ID没有返回. 解决办法有三种: 1.element ...

随机推荐

  1. SQL优化汇总

    今天面某家公司,然后问我SQL优化,感觉有点忘了,今天特此总结一下: 总结得是分两方面:索引优化和查询优化: 一. 索引优化: 1. 独立的列 在进行查询时,索引列不能是表达式的一部分,也不能是函数的 ...

  2. Leetcode(3)-无重复字符的最长子串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. 给定 &q ...

  3. C++动态申请一维数组和二维数组

    在平时的编程过程中,我们经常会用到数组来存放数据,我们可以直接申请足够大空间的数组来保证数组访问不会越界,但是即便这样,我们依然不能保证空间分配的足够,而且非常的浪费空间.有时候我们需要根据上面得到的 ...

  4. IT-ebooks free download website & IT 电子书籍免费下载网站

    free ebooks of programming 1. http://www.it-ebooks.info/ http://www.it-ebooks-api.info/ 2. http://ww ...

  5. Open Collective

    Open Collective Open Collective is an online funding platform for open and transparent communities. ...

  6. Python & file operation mode

    Python & file operation mode create/read/write/append mode https://docs.python.org/3/library/fun ...

  7. Java Hipster

    Java Hipster JHipster JHipster is a development platform to quickly generate, develop, & deploy ...

  8. gitpod & free online ide

    gitpod & free online ide Dev Environments as Code for modern DevOps teams. https://www.gitpod.io ...

  9. taro 如何展示多行文本 省略号

    taro 如何展示多行文本 省略号 webkit-box-orient: vertical; See the Pen Pure CSS multiline text with ellipsis by ...

  10. Echarts制作一张全球疫情图

    一.获取全球疫情数据 1)获取API 使用用友提供的新冠肺炎实时数据,登录注册之后可以免费使用. 2)点击用户信息 这里的AIPCODE,复制并保存,用于后续的使用. 3)API的使用 用友有提供一个 ...