热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用EasyUITree控件

树状数据结构是项目开发中经常使用的,使用EasyUI开发前端界面最好使用EasyUI提供的Tree控件,使用起来非常容易。树状数据结构遍历有两种方法&

树状数据结构是项目开发中经常使用的,使用EasyUI开发前端界面最好使用EasyUI提供的Tree控件,使用起来非常容易。

树状数据结构遍历有两种方法:
一是:使用递归调用穷举数据结构的每一级节点,编程比较麻烦,
二是:使用异步加载的方式,每次点击鼠标展开当前节点的下一级节点,编程比较简单。


EasyUI树控件

在这里插入图片描述


创建页面使用布局管理器分为左右两部分




在页面就绪事件初始Tree控件数据

根节点使用了静态数据



设计数据库表

CMD>mysql -uroot -prootcreate database w1 character set utf8;use w1; //打开数据库w1
create table ORGMODEL_ORG (id int auto_increment primary key,name varchar(200),priority int,parentid int,path varchar(200),fullname varchar(500));charset gbk;insert into ORGMODEL_ORG(name,priority,parentid) values('集团公司',0,null);
insert into ORGMODEL_ORG(name,priority,parentid) values('销售公司',1,1);
insert into ORGMODEL_ORG(name,priority,parentid) values('生产公司',2,1);
insert into ORGMODEL_ORG(name,priority,parentid) values('财务部',1,2);
insert into ORGMODEL_ORG(name,priority,parentid) values('审计科',1,4);
insert into ORGMODEL_ORG(name,priority,parentid) values('生产车间',2,3);

在这里插入代码片

设计部门实体类OrgInfo

package com.test.model;import java.io.Serializable;
import java.sql.Date;/*** 部门实体类* @author ChenQiXiang**/
public class OrgInfo implements Serializable{//部门ID,部门的唯一标识private Integer id = null;//部门名称private String name = null;//部门父IDprivate Integer parentId = null;//部门排序private Integer priority = 0;//部门路径private String path = null;//部门全名称private String fullName = null;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getParentId() {return parentId;}public void setParentId(Integer parentId) {this.parentId = parentId;}public Integer getPriority() {return priority;}public void setPriority(Integer priority) {this.priority = priority;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public String getFullName() {return fullName;}public void setFullName(String fullName) {this.fullName = fullName;}
}

树控件数据类

package com.test.model;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class TreeInfo {public String id = null;private String text = null;private String iconCls = null;private String url = null;private String state = null;private Map attributes = new HashMap();public String getId() {return id;}public void setId(String id) {this.id = id;}public String getText() {return text;}public void setText(String text) {this.text = text;}public String getIconCls() {return iconCls;}public void setIconCls(String iconCls) {this.iconCls = iconCls;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getState() {return state;}public void setState(String state) {this.state = state;}public void addProp(String key,String value){attributes.put(key,value);}public Map getAttributes() {return attributes;}public void setAttributes(Map attributes) {this.attributes = attributes;}
}

设计操作数据库方法

Mapper.xmlinsert into ORGMODEL_ORG(name,parentId,priority,path,fullname)values(#{name},#{parentId},#{priority},#{path},#{fullName})Mapper接口//获取所有部门列表public List getOrgList();//根据部门ID获取部门对象public OrgInfo getOrgById(@Param("id") Integer id);//根据部门ID获取下属部门列表public List getChildOrg(@Param("id") Integer id);//获取根部门public OrgInfo getRootOrg();public void saveOrg(OrgInfo oi);服务接口/*** 获取所有部门列表* * @return List*/public List getOrgList() throws Exception;/*** 根据部门ID获取部门对象* @param id 部门ID* @return List*/public OrgInfo getOrgById(@Param("id") Integer id) throws Exception;//根据部门ID获取下属部门列表public List getChildOrg(Integer id);//获取根部门public OrgInfo getRootOrg();public void saveOrg(OrgInfo oi);服务实现类@Overridepublic List getOrgList() throws Exception{try{return mapper.getOrgList();}catch(Exception e){throw e;}}@Overridepublic OrgInfo getOrgById(Integer id) throws Exception{try{return mapper.getOrgById(id);}catch(Exception e){throw e;}}@Overridepublic List getChildOrg(Integer id) {try{return mapper.getChildOrg(id);}catch(Exception e){e.printStackTrace();}return null;}@Overridepublic OrgInfo getRootOrg() {try{return mapper.getRootOrg();}catch(Exception e){e.printStackTrace();}return null;}@Overridepublic void saveOrg(OrgInfo oi) {try{mapper.saveOrg(oi);}catch(Exception e){e.printStackTrace();}}

定义页面Tree控件节点展开前事件

每次展开接口从后台加载当前节点的下一级节点,并添加到本节点上。

$(document).ready(function(){$('#tree').tree({data:[{'id':'1','text':'集团公司','state':'closed','attributes':{"priority":null,"parentId":null,"isload":"false"}}],onBeforeExpand:function(node,param){if(node.attributes.isload=='false'){$.ajaxSettings.async = false;var url = '/orgchild?id='+node.id;$.get(url,function(data){$('#tree').tree('append', {parent: node.target,data: data});})$.ajaxSettings.async = true;node.attributes.isload = 'true';}}})});

Controller层添加相应的

方法

@ResponseBody@RequestMapping("/orgroot")public List orgroot(){OrgInfo root = orgmodel.getRootOrg();List trees = new ArrayList();TreeInfo rootTree = new TreeInfo();rootTree.setId(root.getId().toString());rootTree.setText(root.getName());rootTree.setState("closed");Map attributes = new HashMap();attributes.put("parentId",root.getParentId());attributes.put("priority", root.getPriority());attributes.put("path", "/");attributes.put("fullName", "/"+root.getName());rootTree.setAttributes(attributes);trees.add(rootTree);return trees;}@ResponseBody@RequestMapping("/orgchild")public List orgchild(Integer id){List child = orgmodel.getChildOrg(id);List trees = new ArrayList();for(OrgInfo oi:child){TreeInfo rootTree = new TreeInfo();rootTree.setId(oi.getId().toString());rootTree.setText(oi.getName());rootTree.setState("closed");Map attributes = new HashMap();attributes.put("parentId", oi.getParentId());attributes.put("priority", oi.getPriority());attributes.put("path", oi.getPath());attributes.put("fullName", oi.getFullName());attributes.put("isload", "false");rootTree.setAttributes(attributes);trees.add(rootTree);}return trees;}

点击节点事件处理

$(document).ready(function(){$('#tree').tree({data:[{'id':'1','text':'集团公司','state':'closed','attributes':{"priority":null,"parentId":null,"isload":"false"}}],onBeforeExpand:function(node,param){if(node.attributes.isload=='false'){$.ajaxSettings.async = false;var url = '/orgchild?id='+node.id;$.get(url,function(data){$('#tree').tree('append', {parent: node.target,data: data});})$.ajaxSettings.async = true;node.attributes.isload = 'true';}},onExpand: function(node){},onClick: function(node){$('#id').val(node.id);$('#name').textbox('setValue',node.text);$('#priority').textbox('setValue',node.attributes.priority);$('#parentId').val(node.attributes.parentId);}})});

添加Form表单




添加子节点功能

JS事件
Controller保存方法@ResponseBody@RequestMapping("/orgsubsave")public boolean orgsubsave(OrgInfo oi){Integer parentId = oi.getId();oi.setId(null);oi.setParentId(parentId);boolean rtn = sendmsg.sendSyncMsg(oi);return rtn;}

在这里插入图片描述

代码下载
https://pan.baidu.com/s/1HGcg90JP3HJVxOjqFreY_w


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
author-avatar
在这里啊
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有