树型下拉框实现

代码参考:http://www.java2000.net/p944

public class SelectTree {
	// 编号
	private String id;

	// 标题
	private String title;

	// 下级列表
	private List<SelectTree> children = new ArrayList<SelectTree>();

	// 上级,顶层为null
	private SelectTree parent;

	// 前一个节点
	private SelectTree prev;

	// 后一个节点
	private SelectTree next;

	// 当前处理的节点
	private SelectTree current;

	/**
	 * 默认的构造器
	 */
	public SelectTree() {
	}

	/**
	 * 推荐使用的构造器
	 * 
	 * @param id
	 *            编号
	 * @param title
	 *            文本
	 */
	public SelectTree(String id, String title) {
		this.id = id;
		this.title = title;
	}

	/**
	 * 增加一个下属。
	 * 
	 * 自动对应上级和兄弟结点
	 * 
	 * @param f
	 *            被增加的节点
	 */
	public void addChild(SelectTree f) {
		children.add(f);
		f.setParent(this);
		if (current != null) {
			current.next = f;
		}
		f.prev = current;
		current = f;
	}

	/**
	 * 输出为下拉列表的方法
	 * 
	 * @param selectedId
	 *            被选中的编号
	 * @return 下拉列表的字符串。可以直接放到<select></select>里面
	 */
	public String getOption(String selectedId) {
		return "<option value=''>" + toStringOption("", "", selectedId);
	}

	/**
	 * 输出为Text的方法。
	 * 
	 * @param lftStr
	 *            左侧额外的字符串
	 * @param append
	 *            右侧显示的字符串
	 * @return 文本形式的字符串
	 */
	public String toStringText(String lftStr, String append) {
		StringBuilder b = new StringBuilder();
		b.append(append + title);
		b.append("\n");
		if (children.size() > 0) {
			for (int i = 0; i < children.size() - 1; i++) {
				b.append(lftStr
						+ children.get(i).toStringText(lftStr + "│", "├"));
			}
			b.append(lftStr
					+ children.get(children.size() - 1).toStringText(
							lftStr + " ", "└"));
		}
		return b.toString();
	}

	public static void main(String[] args) {
		 	 		f1_1_1.addChild(f1_1_1_1);
		SelectTree f1_1_1_2 = new SelectTree("1112",
				"娱乐2");
		f1_1_1.addChild(f1_1_1_2);
		SelectTree f1_2 = new SelectTree("12", "辅助工具");
		f1.addChild(f1_2);
		SelectTree f2 = new SelectTree("2",
				"My Documents ");
		root.addChild(f2);
		SelectTree f3 = new SelectTree("3",
				"My Documents2 ");
		SelectTree f5 = new SelectTree("5",
		"My Documents5 ");
		SelectTree f6 = new SelectTree("6",
		"My Documents6 ");
		f5.addChild(f6);
		f3.addChild(f5);
		root.addChild(f3);
		System.out.println(root.toStringText(" ", ""));
		System.out.println(root.getOption("0"));
		System.out.println(f1_1_1_2.getPrev().getTitle());
		System.out.println(f1_1_1_2.getPrev().getParent().getTitle());
	}

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

	public String getId() {
		return id;
	}

	/**
	 * 得到下一个兄弟结点。
	 * 
	 * @return 如果是最后一个,则返回null
	 */
	public SelectTree getNext() {
		return next;
	}

	public SelectTree getParent() {
		return parent;
	}

	/**
	 * 得到前一个兄弟结点。
	 * 
	 * @return 如果是第一个,则返回null
	 */
	public SelectTree getPrev() {
		return prev;
	}

	public String getTitle() {
		return title;
	}

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

	public void setNext(SelectTree next) {
		this.next = next;
	}

	public void setParent(SelectTree parent) {
		this.parent = parent;
	}

	public void setPrev(SelectTree prev) {
		this.prev = prev;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * 构造下拉列表.
	 * 
	 * @param lftStr
	 *            左侧的字符
	 * @param append
	 *            增加的字符
	 * @param idSelected
	 *            被选中的编号
	 * @return 下拉列表字符串
	 */
	private String toStringOption(String lftStr, String append, String idSelected) {
		StringBuilder b = new StringBuilder();
		b.append(append + title + "</option>");
		b.append("\n");
		if (children.size() > 0) {
			for (int i = 0; i < children.size() - 1; i++) {
				b.append("<option value='"
						+ children.get(i).getId()
						+ "'"
						+ (idSelected.equals(children.get(i).getId()) ? " selected"
								: "")
						+ ">"
						+ lftStr
						+ children.get(i).toStringOption(lftStr + "│", "├",
								idSelected));
			}
			b.append("<option value='"
							+ children.get(children.size() - 1).getId()
							+ "'"
							+ (idSelected.equals(children.get(children.size() - 1).getId()) ? " selected" : "")
							+ ">"
							+ lftStr
							+ children.get(children.size() - 1).toStringOption(
									lftStr + "&nbsp;", "└", idSelected));
		}
		return b.toString();
	}
}


  Action:
	public String rNavFind() {
		// 根节点
		SelectTree root = new SelectTree("0", "权限列表");
              //service递归产生节点
		rbacService.getCoreFuncsByPid(root, "0");
		coreFuncsOptionStr = root.getOption(coreTree.getFunc_id());
		
		coreTree = this.rbacService.findTreeById(id);
		hasChildren = this.treeNodeService.hasChildren(id);

		return "edit";
	}


 Service:
	public void getCoreFuncsByPid(SelectTree currentSelectTree,
			String pid) {
		List<CoreFunc> list = this.rbacDao.getCoreFuncsByPid(pid);
		SelectTree selectTree;
		int cnt = 0;
		for (CoreFunc cf : list) {
			selectTree = new SelectTree(cf.getId(), cf.getName());
			currentSelectTree.addChild(selectTree);
			cnt = this.rbacDao.getChildrenCountByPid(cf.getId());
			if (cnt == 0) {
				continue;
			} else {
				getCoreFuncsByPid(selectTree, selectTree.getId());
			}
		}
	}

 JSP:
   <select name="funcId">						        
     <s:property value="coreFuncsOptionStr" escape="false" />
   </select>

你可能感兴趣的:(jsp,.net,F#)