private void bindData() throws Exception { if (this.DataSource == null) { throw new RuntimeException("必须在bindData方法中设定DataSource"); } this.items.clear(); this.root = new TreeItem(); this.root.setID("_TreeRoot"); this.root.setParentID(""); this.root.setRoot(true); this.root.setText(this.rootText); this.root.setAction(this); this.root.setLevel(0); this.root.setAttribute("onMouseOver", this.onMouseOver); this.root.setAttribute("onContextMenu", this.onContextMenu); this.root.setAttribute("onClick", this.onClick); this.root.setAttribute("onMouseOut", this.onMouseOut); this.items.add(this.root); Mapx map = new Mapx(); for (int i = 0; i < this.DataSource.getRowCount(); i++) map.put( this.DataSource.getString(i, this.IdentifierColumnName), this.DataSource.getString(i, this.ParentIdentifierColumnName)); try { TreeItem last = null; for (int i = 0; i < this.DataSource.getRowCount(); i++) { DataRow dr = this.DataSource.getDataRow(i); String parentID = dr.getString(this.ParentIdentifierColumnName); if ((XString.isEmpty(parentID)) || (!map.containsKey(parentID))) { TreeItem item = new TreeItem(); item.setData(dr); item.parseHtml(getItemInnerHtml(dr)); item.setAction(this); item.setID(dr.getString(this.IdentifierColumnName)); item.setParentID(parentID); if (this.lazyLoad) { item.setLevel(this.parentLevel + 1); item.setLevelStr((String) getParams().get("LevelStr")); } else { item.setLevel(1); } item.setParent(this.root); this.items.add(item); addChild(item); last = item; } } if (last != null) last.setLast(true); } catch (Exception e) { e.printStackTrace(); } }
public void addChild(TreeItem parent) throws Exception { boolean childFlag = false; TreeItem last = null; int index = -1; for (int i = this.items.size() - 1; i >= 0; i--) { if (this.items.get(i) == parent) { index = i; break; } } ArrayList list = new ArrayList(); for (int i = 0; i < this.DataSource.getRowCount(); i++) { DataRow dr = this.DataSource.getDataRow(i); String pid = dr.getString(this.ParentIdentifierColumnName); String id = dr.getString(this.IdentifierColumnName); if ((parent.getID().equals(pid)) && (!XString.isEmpty(id)) && (!id.equals(pid))) { childFlag = true; if ((parent.getLevel() >= this.level) && ((!this.lazyLoad) || (!this.expand))) { parent.setLazy(this.lazy); parent.setExpanded(false); parent.setBranch(childFlag); return; } TreeItem item = new TreeItem(); item.setData(dr); item.parseHtml(getItemInnerHtml(dr)); item.setAction(this); item.setID(dr.getString(this.IdentifierColumnName)); item.setParentID(parent.getID()); item.setLevel(parent.getLevel() + 1); item.setParent(parent); if (this.lazyLoad) { item.setLevelStr((String) getParams().get("LevelStr")); } list.add(item); this.items.add(index + list.size(), item); last = item; } } for (int i = 0; i < list.size(); i++) { addChild((TreeItem) list.get(i)); } if (last != null) { last.setLast(true); } if ((!this.lazy) && (parent.getLevel() + 1 == this.level)) { parent.setExpanded(false); } parent.setBranch(childFlag); }