日期:2014-05-16  浏览次数:20329 次

Extjs4.1 TreePanel 的增删改查与拖拽排序
用了一天的时间完成了对Extjs TreePanel的 增删改查与拖拽排序

一来对自己写的代码做一个总结,二来给有需要的人一些帮组,

如果代码有什么BUG或者更好的建议,欢迎大家留言


先看效果图




/**
 * 定义树节点
 */
public class TreeNode {

	//ID
	private int id;
	
	//展示数时显示的文本
	private String text;
	
	//是否是叶子节点
	private boolean leaf;
	
	//子节点的集合
	//(@JsonInclude(Include.NON_NULL)序列化时的标记 如果为空则不参与序列化)
	@JsonInclude(Include.NON_NULL)  
	private List<TreeNode> children;

	public TreeNode() {}
	
	public TreeNode(int id, String text, boolean leaf) {
		super();
		this.id = id;
		this.text = text;
		this.leaf = leaf;
	}

	public TreeNode(int id, String text, boolean leaf, List<TreeNode> children) {
		super();
		this.id = id;
		this.text = text;
		this.leaf = leaf;
		this.children = children;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
}



package com.sshe.operation.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sshe.base.controller.BaseController;
import com.sshe.operation.entity.TreeNode;

/**
 * 控制层,我用的Spring3 MVC
 * 4个方法分别与Extjs的API对应
 * api: {
 *	    create: 'TreeApiController.do?method=createTreeNode',
 *	    read: 'TreeApiController.do?method=queryTreeList',
 *	    update: 'TreeApiController.do?method=updateTreeNode',
 *	    destroy: 'TreeApiController.do?method=destroyTreeNode'
 * }
 */
@Controller
@RequestMapping("/TreeApiController.do")
public class TreeApiController extends BaseController {

	/**
	 * 查询的方法
	 * 对应 read: 'TreeApiController.do?method=queryTreeList'
	 */
	@ResponseBody
	@RequestMapping(params="method=queryTreeList")
	public Object queryTreeList(ModelMap map) {
		System.out.println(map);
		JsonResult result = new JsonResult();
		result.setSuccess(true);
		result.setData(getTreeList());
		return getTreeList();
	}
	
	/**
	 * 新增方法
	 * 对应create: 'TreeApiController.do?method=createTreeNode'
	 */
	@ResponseBody
	@RequestMapping(params="method=createTreeNode")
	public Object createTreeNode(ModelMap map) {
		System.out.println(map);
		JsonResult result = new JsonResult();
		result.setSuccess(true);
		return result;
	}
	
	/**
	 * 更新的方法
	 * 对应 update: 'TreeApiController.do?method=updateTreeNode'
	 */
	@ResponseBody
	@RequestMapping(params="method=updateTreeNode")
	public Object updateTreeNode(ModelMap map) {
		System.out.println(map);
		JsonResult result = new JsonResult();
		result.setSuccess(true);
		return result;
	}
	
	/**
	 * 删除的方法
	 * 对应 destroy: 'TreeApiController.do?method=destroyTreeNode'
	 */
	@ResponseBody
	@RequestMapping(params="method=destroyTreeNode")
	public Object destroyTreeNode(ModelMap map) {
		System.out.println(map);
		JsonResult result = new JsonResult();
		result.setSuccess(true);
		return result;
	}
	
	/**
	 * 测试数据
	 */
	public List<TreeNode> getTreeList() {
		List<TreeNode> list = new ArrayList<TreeNode>();
		
		TreeNode java = new TreeNode(1, "java-1", false, new ArrayList<TreeNode>());
		TreeNode hibernate = new TreeNode(2, "hibernate-2", true);
		TreeNode spring = new TreeNode(3, "spring-3", true);
		TreeNode struts = new TreeNode(4, "struts-4", true);
		java.getChildren().add(hibernate);
		java.getChildren().add(spring);
		java.getChildren().add(struts);