EasyUI实现树状下拉框

部门表的树状下拉框实现
在这里插入图片描述

public class DepartmentForTree {
	
	private int id;
	private String text;
	private int pid;
	private List<DepartmentForTree> children 
		= new ArrayList<DepartmentForTree>();
	
	public DepartmentForTree() {
		super();
	}
	public DepartmentForTree(int id, String text) {
		super();
		this.id = id;
		this.text = text;
	}
	public DepartmentForTree(int id, String text, int pid) {
		super();
		this.id = id;
		this.text = text;
		this.pid = pid;
	}
	public DepartmentForTree(int id, String text,
			List<DepartmentForTree> children) {
		super();
		this.id = id;
		this.text = text;
		this.children = children;
	}
	
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	
	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 List<DepartmentForTree> getChildren() {
		return children;
	}
	public void setChildren(List<DepartmentForTree> children) {
		this.children = children;
	}
	@Override
	public String toString() {
		return "DepartmentForTree [id=" + id + ", text=" + text + ", children="
				+ children + "]";
	}
}

service实现方法

public class DepartmentImpl implements IDepartmentBiz{
	private  DepartmentDao dao = new DepartmentDao();
	public List<DepartmentForTree> queryAllDepartmentsForTree() {
		
		String sql = "SELECT "
				+ " departmentID id , departmentName text "
				+ ", parentID pid "      //departmentID是二级部门ID parentID是一级部门ID
				+ " FROM department order by parentID asc";
		
		List<DepartmentForTree> departments =
			dao.query(sql, DepartmentForTree.class, new Object[]{});
		
		List<DepartmentForTree> ones = new ArrayList<DepartmentForTree>();
		List<DepartmentForTree> twos = new ArrayList<DepartmentForTree>();
		
		for (DepartmentForTree department : departments) {
			if(department.getPid() == 0)
			{
				
				ones.add( department );
			}else
			{
				twos.add( department);
			}
		} 
		
		for (DepartmentForTree departmentOne : ones) {
			for (DepartmentForTree departmentTwo : twos) {
				if( departmentOne.getId() ==
						departmentTwo.getPid())
				{
					departmentOne.getChildren()
						.add(departmentTwo);
				}
			}
		}
		
		return ones;
	}

Servlet实现方法

private void queryAll(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		List<DepartmentForTree> departments = biz.queryAllDepartmentsForTree();
		departments.add( 
				new DepartmentForTree( 0 , "全部" ) );
		String jsonString = JSON.toJSONString(departments);
		PrintWriter out = response.getWriter();
		out.print(jsonString);
		out.flush();
		out.close();
		
	}

combotree方法

$(
	function () {
		$("#departmentID").combotree(
		{
        	url:'http://localhost:8080/KQSYS/DepartmentController/queryAllDepartmentsForTree',
        	height:26,
        	width:'150px',
        	onBeforeSelect : function(note)
        	{
        		
        		var rows = note.children;
        		if(rows.length>0)
        		{
        			$("#departmentID").treegrid("unselect");
        		}
        	}
        	,onSelect:function () {
           		  	
        	}

		});
		
		
	});

有错误请指正 谢谢。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值