之所以使用懒加载是为了提高性能,而且只有在懒加载模式下默认会给所有显示节点设置展开按钮。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. hdu 5883

    Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM ...

  2. Leetcode(2)-两数相加(包含链表操作的注意事项)

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  3. springboot demo(一)快速开始

    快速入门 maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.26以及一些工程基本信息,点击" ...

  4. bash variables plus operator All In One

    bash variables plus operator All In One Errors missing pass params #!/usr/bin/env bash # echo emoji ...

  5. Apple Watch Series 6 屏幕误触放大后无法还原问题和解决方案

    Apple Watch Series 6 屏幕误触放大后无法还原问题和解决方案 shit Apple,只能放大,不能缩小! 解决方案 关闭缩放功能 https://support.apple.com/ ...

  6. TCP 协议三次握手过程分析

    TCP 协议三次握手过程分析 TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...

  7. setTimeout 实现原理, 机制

    setTimeout 实现原理, 机制 JS 执行机制说起 浏览器(或者说 JS 引擎)执行 JS 的机制是基于事件循环. 由于 JS 是单线程,所以同一时间只能执行一个任务,其他任务就得排队,后续任 ...

  8. auto open Chrome DevTools in the command line

    auto open Chrome DevTools in the command line --auto-open-devtools-for-tabs # macOS $ /Applications/ ...

  9. no code form generator

    no code form generator 无代码,表单生成器 H5 Drag & Drop UI => codes click copy demo https://www.forms ...

  10. js trigger click event & dispatchEvent & svg element

    js trigger click event & dispatchEvent & svg element but svg element not support trigger cli ...