浏览 3052 次
锁定老帖子 主题:zk生成多级树
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-14
最后修改:2010-12-14
<zk> <window id="win" border="normal" contentStyle="padding:0px" apply="account.DengJiComposer"> <borderlayout height="600px" width="100%"> <west> <tree width="200px" height="600px" id="tree" zclass="z-dottree"> <treecols sizable="true"> <treecol label="项目列表"> </treecol> </treecols> </tree> </west> <center> <include id="include"></include> </center> </borderlayout> </window> </zk> 后台类 package account; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.Include; import org.zkoss.zul.Tree; public class DengJiComposer extends GenericForwardComposer{ private Tree tree; @SuppressWarnings("unused") private Include include; public void doAfterCompose(Component comp) throws Exception{ super.doAfterCompose(comp); tree.setModel(new MyTreeModel()); tree.setTreeitemRenderer(new MyTreeitemRenderer()); } } MyTreeModel类代码 package account; import java.util.ArrayList; import java.util.List; import account.bean.Item; import org.zkoss.zul.TreeModel; import org.zkoss.zul.event.TreeDataListener; public class MyTreeModel implements TreeModel{ private List<Item>model; AccDAO acc = new AccDAO(); public MyTreeModel(){ model = acc.findAllxm(); } @Override public void addTreeDataListener(TreeDataListener l) { // TODO Auto-generated method stub } @Override public Object getChild(Object parent, int index) { // TODO Auto-generated method stub if(parent instanceof List) return ((List)parent).get(index); else if(parent instanceof Item){ List<Item> xmList = acc.getXmChild((Item)parent); return xmList.get(index); } else return null; } @Override public int getChildCount(Object parent) { // TODO Auto-generated method stub if(parent instanceof List) return ((List)parent).size(); else if(parent instanceof Item) return (int)acc.getChildCount((Item)parent); else return 0; } @Override public int[] getPath(Object parent, Object lastNode) { // TODO Auto-generated method stub return null; } @Override public Object getRoot() { // TODO Auto-generated method stub List<Item>rootlist = new ArrayList(); for(Item xm:model){ if(xm.getParentId()==null) rootlist.add(xm); } return rootlist; } @Override public boolean isLeaf(Object node) { // TODO Auto-generated method stub return acc.checkIsLeaf((Item)node); } @Override public void removeTreeDataListener(TreeDataListener l) { // TODO Auto-generated method stub } } MyTreeitemRenderer类 package account; import account.bean.Item; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; import org.zkoss.zul.Include; import org.zkoss.zul.Treeitem; import org.zkoss.zul.TreeitemRenderer; public class MyTreeitemRenderer implements TreeitemRenderer{ private boolean addDoubleClick = false; public MyTreeitemRenderer(){ this(true); } public MyTreeitemRenderer(boolean addDoubleClick){ this.addDoubleClick = addDoubleClick; } @Override public void render(final Treeitem item, Object data) throws Exception { // TODO Auto-generated method stub Item ite = (Item)data; item.setLabel(ite.getItemname()); item.setValue(ite); if(new AccDAO().checkIsLeaf(ite)){ item.getTreerow().addEventListener(Events.ON_DOUBLE_CLICK, new EventListener(){ @Override public void onEvent(Event event) throws Exception { // TODO Auto-generated method stub Include include = (Include) item.getFellow("include"); include.setSrc(null); include.setSrc("/dengji.zul"); include.setDynamicProperty("ite", item.getValue()); } }); } } } DAO相关查询 public List<Item>findAllxm() { Session sess=HibernateSessionFactory.getSession(); try{ return sess.createQuery("from Item").list(); }catch(Exception e){ e.printStackTrace(); } finally{ sess.close(); } return null; } public List<Item> getXmChild(Item xm){ try { Session sess = HibernateSessionFactory.getSession(); String hql = "from Item where parentId=?"; Query query = sess.createQuery(hql); query.setInteger(0, xm.getItemId()); return query.list(); } finally{ HibernateSessionFactory.closeSession(); } }public long getChildCount(Item xm){ public boolean checkIsLeaf(Item xm){ try { Session sess = HibernateSessionFactory.getSession(); String hql = "select count(id) from Item where parentId=?"; Query query = sess.createQuery(hql); query.setInteger(0, xm.getItemId()); return ((Long)query.uniqueResult()).longValue()==0; } finally{ HibernateSessionFactory.closeSession(); } } Treeitem treeitem=(Treeitem)event.getOrigin().getTarget(); if(treeitem.getTreechildren().getChildren().size()>0){ return; } tableLists=tableDAO.getTableName((String) treeitem.getValue()); for(Iterator iter=tableLists.iterator();iter.hasNext();){ String name=(String)iter.next(); final Treeitem item=new Treeitem(); item.setValue(name); item.setLabel(name); item.setParent(treeitem.getTreechildren()); if(tableTree.isCheckmark()){ item.isCheckable(); } item.addEventListener(Events.ON_DOUBLE_CLICK, new EventListener() { public void onEvent(Event event) throws Exception { } }); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |