EasyUI Editable Tree

效果如图:

Create Tree

<ul id="tt"></ul>
$(‘#tt‘).etree({
	url: ‘tree_data.json‘,
	createUrl: ...,
	updateUrl: ...,
	destroyUrl: ...,
	dndUrl: ...
});

设置url,createUrl,updateUrl,destroyUrl,dndUrl属性来自动同步数据到服务器

url:  返回树的数据

createUrl:  当创建一个新的节点,tree将传入名为parentId即表示父节点ID的参数

updateUrl:  当更新一个节点,将传入id和text参数到服务器

destroyUrl:  当销毁一个节点,传入id参数

dndUrl:  当拖放节点,将传入以下参数到服务器。ID:被拖动的ID,targetId:被拖至到的ID

Demo:

<body>
     <a href="#" onclick="javascript:$(‘#tt‘).etree(‘create‘)">Create</a>
     <a href="#" onclick="javascript:$(‘#tt‘).etree(‘edit‘)">Edit</a>
     <a href="#" onclick="javascript:$(‘#tt‘).etree(‘destroy‘)">Destroy</a>
     <ul id=tt></ul>
</body>
<script type="text/javascript">
     $(‘#tt‘).etree({
	  url: ‘treeLoad.action‘,
	  createUrl: ‘treeCreate.action‘,
	  updateUrl: ‘treeUpdate.action‘,
	  destroyUrl: ‘treeDestroy.action‘,
	  dndUrl: ‘treeDnd.action‘
     });
</script>

struts.xml

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="tree_json" extends="json-default">
		<action name="treeLoad" method="treeLoad" class="com.home.web.TreeAction">
			<result type="json">
				<param name="root">treeNodes</param>
			</result>
		</action>
		<action name="treeCreate" method="treeCreate" class="com.home.web.TreeAction">
			<result type="json">
				<param name="root">treeNodes</param>
			</result>
		</action>
		<action name="treeUpdate" method="treeUpdate" class="com.home.web.TreeAction">
			<result type="json">
				<param name="root">treeNodes</param>
			</result>
		</action>
		<action name="treeDestroy" method="treeDestroy" class="com.home.web.TreeAction">
			<result type="json">
				<param name="root">treeNodes</param>
			</result>
		</action>
		<action name="treeDnd" method="treeDnd" class="com.home.web.TreeAction">
			<result type="json">
				<param name="root">treeNodes</param>
			</result>
		</action>
	</package>
</struts>

需要封装对象Tree

public class TreeNode {
	private static final long serialVersionUID = 1L;
	private String id;   // 节点id
	private String text; // 显示的节点文本
	private String state = "open"; // 节点状态,‘open‘或者 ‘closed‘,默认是 ‘open‘
	private boolean checked;       // 指明检查节点是否选中.

	public TreeNode() {
	}

	public TreeNode(String id, String text, String state, boolean checked) {
		this.id = id;
		this.text = text;
		this.state = state;
		this.checked = checked;
	}

	//省略setXXX(),getXXX()

}

Action方法实现

/**
 * 查询数据使用JDBC进行操作
 *
 */
public class TreeAction {
	private List<TreeNode> treeNodes = new ArrayList<TreeNode>(); // 返回的JSON数据
	private String id; // 树组件使用的ID
	private String parentId; // 树父ID
	private String text; // 显示文本
	private String targetId; // 拖拽目标ID

	/**
	 * 树展现
	 *
	 * @return
	 */
	public String treeLoad() {

		Statement sta = null;
		ResultSet rs = null;
		try {
			Connection conn = ConnectionManager.getConnection();
			sta = conn.createStatement();
			String sql = "";
			if (id == null) { // 如果id为null或0则是根节点
				sql = "select * from easyui_tree where parentid = ‘‘ or parentid = ‘0‘";
			} else { // 查询下面的子节点
				sql = "select * from easyui_tree where parentid = " + id;
			}
			rs = sta.executeQuery(sql);

			while (rs.next()) {
				String id = rs.getString("id");
				String name = rs.getString("name");
				TreeNode node = new TreeNode();
				node.setId(id);
				node.setText(name);
				node.setChecked(false);
				// 判断是否有子节点,如果有则closed 否则open
				if (isChildrenNode(id)) {
					node.setState("closed");
				} else {
					node.setState("open");
				}
				treeNodes.add(node);
			}
			// 关闭所有资源
			ConnectionManager.closeAll(rs, sta, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return "success";
	}

	/**
	 * 创建tree
	 *
	 * @return
	 */
	public String treeCreate() {
		Statement sta = null;
		ResultSet rs = null;
		try {
			Connection conn = ConnectionManager.getConnection();
			sta = conn.createStatement();
			//ID为自增,无需插入
			String sql = "insert into easyui_tree(NAME,parentid) values(‘‘,‘" + parentId + "‘)";
			sta.execute(sql);

			// 关闭所有资源
			ConnectionManager.closeAll(rs, sta, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 修改tree
	 *
	 * @return
	 */
	public String treeUpdate() {
		Statement sta = null;
		ResultSet rs = null;
		try {
			Connection conn = ConnectionManager.getConnection();
			sta = conn.createStatement();
			String sql = "update easyui_tree set name = ‘" + text + "‘ where id = ‘" + id + "‘";
			sta.executeUpdate(sql);

			// 关闭所有资源
			ConnectionManager.closeAll(rs, sta, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 删除tree
	 *
	 * @return
	 */
	public String treeDestroy() {
		Statement sta = null;
		ResultSet rs = null;
		try {
			Connection conn = ConnectionManager.getConnection();
			sta = conn.createStatement();
			String sql = "delete from easyui_tree where id = ‘" + id + "‘";
			sta.executeUpdate(sql);

			// 关闭所有资源
			ConnectionManager.closeAll(rs, sta, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 拖拽
	 *
	 * @return
	 */
	public String treeDnd() {
		Statement sta = null;
		ResultSet rs = null;
		try {
			Connection conn = ConnectionManager.getConnection();
			sta = conn.createStatement();
			//将parentid改为拖拽至目标ID
			String sql = "update  easyui_tree set parentid = ‘" + targetId + "‘ where id = ‘" + id + "‘";
			sta.executeUpdate(sql);

			// 关闭所有资源
			ConnectionManager.closeAll(rs, sta, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 判断是否有子节点
	 *
	 * @return
	 */
	public boolean isChildrenNode(String id) {
		Boolean flag = false;
		Statement sta = null;
		ResultSet rs = null;
		try {
			Connection conn = ConnectionManager.getConnection();
			sta = conn.createStatement();
			String sql = "select * from easyui_tree where parentid = " + id;
			rs = sta.executeQuery(sql);
			while (rs.next()) {
				flag = true;
			}
			// 关闭所有资源
			ConnectionManager.closeAll(rs, sta, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	//省略setXXX(),getXXX()方法

}

获取Connection的ConnectionManager封装类参见http://blog.csdn.net/itmyhome1990/article/details/38818449

数据库脚本

create database easyui;
use easyui;

CREATE TABLE easyui_tree(
	id  INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
	NAME VARCHAR(10),
	parentid VARCHAR(10)
);

insert into easyui_tree values(‘1‘,‘北京‘,‘0‘);
insert into easyui_tree values(‘2‘,‘上海‘,‘0‘);
insert into easyui_tree values(‘3‘,‘深圳‘,‘0‘);
insert into easyui_tree values(‘4‘,‘广州‘,‘0‘);

insert into easyui_tree values(‘5‘,‘海淀‘,‘1‘);
insert into easyui_tree values(‘6‘,‘朝阳‘,‘1‘);
insert into easyui_tree values(‘7‘,‘昌平‘,‘1‘);
insert into easyui_tree values(‘8‘,‘西二旗‘,‘5‘);
insert into easyui_tree values(‘9‘,‘上地‘,‘5‘);

项目源码下载:http://download.csdn.net/detail/itmyhome/7856545

转载请注明出处:http://blog.csdn.net/itmyhome1990/article/details/38846521

时间: 2024-10-12 20:37:48

EasyUI Editable Tree的相关文章

做权限树时 使用EasyUI中Tree

符合EasyUI中Tree的Json格式,我们先看一下,格式是如何的 <span style="font-size:18px;">[{ "id":1, "text":"My Documents", "children":[{ "id":22, "text":"Photos", "state":"closed

项目实践——Easyui中tree的使用

树状结构在项目中必不可少,这篇博客来学习一下Easyui的tree. 前台JSP <span style="font-size:14px;"><body> <div id="leftDiv" class="easyui-layout" fit="true"> <div region="center" border="false" border=&

jQuery easyui的tree用法

由于easyui这个框架简化了不少开发过程,经常被拿来开发后台,但是其实有一个树型的控件,用起来稍微复杂一点,总结了一下最基本的用法! 首先是前台添加标签如下: <ul id="tt"> </ul> 其次添加引用包 <link href="~/Content/jquery-easyui-1.3.6/themes/default/easyui.css" rel="stylesheet" /> <link h

递归实现EasyUI中Tree的Json格式

最近在做学校的基础系统的时候前台需要树形的组织结构,由于前台的整体框架都用的是EasyUI,所以只能采用EasyUi中Tree的格式,可是麻烦的是,需要符合EasyUI中Tree的Json格式,我们先看一下,格式是如何的呢? <span style="font-size:18px;">[{ "id":1, "text":"My Documents", "children":[{ "id

EasyUI之tree组件与treegrid组件

最近做了一个使用EasyUI的基于角色的权限分配系统,在这个过程中为了有更好的用户体验(也在网上找了挺多关于该系统的界面),先上我做的截图: 虽然,后台的逻辑上并不难,但是这个EasyUI的tree组件和treegrid组件,确实让我废了一大把力气>>好了废话不多说,开始正题: 一:tree组件 每个节点都具备以下属性: id:节点ID,对加载远程数据很重要. text:显示节点文本. state:节点状态,'open' 或 'closed',默认:'open'.如果为'closed'的时候,

20.easyUI之Tree

1.静态ul+li方式实现 1 <div data-options="region:'center',title:'FastQ'" 2 style="overflow: hidden;"> 3 <ul id="tt" class="easyui-tree"> 4 <li><span>中国</span> 5 <ul> 6 <li><span

EasyUI 左侧 tree 右侧 DataGrid模板

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>组织架构管理</title> <script src="jquery-ea

Easyui中tree组件实现搜索定位功能及展开节点定位

这几天遇到个input + tree  实现搜索功能的需求,在这里贴出来供大家参考下,如果你有更好的实现效果希望不腻赐教! 首先给大家看看效果     小二 上图  : 需要的部件知识: easyui API 方法: select       target        选择一个节点,'target'参数表示节点的DOM对象. expand    target         展开一个节点,'target'参数表示节点的DOM对象.在节点关闭或没有子节点的时候,节点ID的值(名为'id'的参数)

04 jquery easyui 之 Tree

树(Tree)可以在一个空的 <ul> 元素中定义,可使用 javascript 加载数据. 在body中添加ul标签 <ul id="tt"></ul> 然后在使用javascript代码加载数据 使用JSON加载数据 $('#tt').tree({ url:'treedata.json'}); 树的数据格式(Tree Data Format) 每个节点可以包括下列属性: id:节点的 id,它对于加载远程数据很重要. text:要显示的节点文本.