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

    先点下所想改变模式的模块, 然后: Window -> Activate tool window -> docked mode.

  2. X-Frame-Options & iframe & CORS

    X-Frame-Options & iframe & CORS https://github.com/xgqfrms/FEIQA/issues/23 X-Frame-Options i ...

  3. js class static property & public class fields & private class fields

    js class static property class static property (public class fields) const log = console.log; class ...

  4. GitHub 500 error

    GitHub 500 error 无法访问了, GitHub 挂了又! error reports Downdetector Github down? Current service status a ...

  5. qt char与code的相互转换

    QString str = "A"; QChar c = str.at(0); // int v_latin = c.toLatin1(); // 不能转中文 int v_lati ...

  6. Flutter 使用p5

    p5 工作示例 install dependencies: p5: ^0.0.5 main.dart import 'package:flutter/material.dart'; import &q ...

  7. Github 获取仓库的releases API

    API 文档 example: 这将获取所有的版本 https://api.github.com/repos/januwA/flutter_anime_app/releases 最新版本: https ...

  8. Renice INC:全球经济危机持续,2021年红酒市场走向如何?

    2021年,全球经济危机仍在持续,很多国家的经济出现了严重的下滑,不得不以降低利率维持经济.那么,全球经济危机对于红酒市场有什么影响?2021年,红酒市场走势如何呢?近日,美国知名红酒公司伦尼斯公司对 ...

  9. Linux下部署Apache+PHP

    Linux下部署Apache+PHP 编辑于2021-02-19 本文中Apache配置文件目录为/etc/httpd/conf/httpd.conf,PHP安装目录为/usr/local/php 一 ...

  10. Vue使用&nbsp空白占位符

    当有时候需要在页面显示时显示空格时,可以使用 ,但是使用这个占位符时,无论写多少个,就只能显示一个空格.要想显示多个空格进行占位,这种方式显然是可行的,解决方法是使用转义字符. 先看代码: <t ...