前台Tree.zul
<?page title="Tree使用" contentType="text/html;charset=UTF-8"?> <zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zkoss.org/2005/zul" xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul"> <window title="" border="normal" apply="test.treeCtrl"><tree id="tree" pageSize="10" mold="paging" rows="10" ><treecols><treecol label="Name"/><treecol label="Description"/></treecols><treefoot><treefooter label="底部"/></treefoot></tree> </window> </zk>
后台treeCtrl.java:
package test;import java.util.ArrayList; import java.util.List;import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.util.GenericAutowireComposer; import org.zkoss.zul.DefaultTreeModel; import org.zkoss.zul.DefaultTreeNode; import org.zkoss.zul.Tree; import org.zkoss.zul.Treecell; import org.zkoss.zul.Treeitem; import org.zkoss.zul.TreeitemRenderer; import org.zkoss.zul.Treerow;public class treeCtrl extends GenericAutowireComposer<Component>{private static final long serialVersionUID = 1L;private Tree tree;@Overridepublic void doAfterCompose(Component comp) throws Exception {// TODO Auto-generated method stubsuper.doAfterCompose(comp);DefaultTreeModel<FileInfo> model = new DefaultTreeModel<FileInfo>(getFileInfoTreeData());tree.setModel(model);tree.setItemRenderer(new FileInfoRenderer());}private DefaultTreeNode<FileInfo> getFileInfoTreeData() {List<DefaultTreeNode<FileInfo>> inner3 = new ArrayList<DefaultTreeNode<FileInfo>>();inner3.add(new DefaultTreeNode<FileInfo>(new FileInfo("zcommon.jar", "ZK Common Library")));inner3.add(new DefaultTreeNode<FileInfo>(new FileInfo("zk.jar", "ZK Core Library")));List<DefaultTreeNode<FileInfo>> inner2 = new ArrayList<DefaultTreeNode<FileInfo>>();inner2.add(new DefaultTreeNode<FileInfo>(new FileInfo("/lib", "ZK Libraries"), inner3));inner2.add(new DefaultTreeNode<FileInfo>(new FileInfo("/src", "Source Code")));inner2.add(new DefaultTreeNode<FileInfo>(new FileInfo("/xsd", "XSD Files")));List<DefaultTreeNode<FileInfo>> inner1 = new ArrayList<DefaultTreeNode<FileInfo>>();inner1.add(new DefaultTreeNode<FileInfo>(new FileInfo("/doc", "Release and License Notes")));inner1.add(new DefaultTreeNode<FileInfo>(new FileInfo("/dist", "Distribution"), inner2));return new DefaultTreeNode<FileInfo>(null, inner1);}public class FileInfoRenderer implements TreeitemRenderer<DefaultTreeNode<FileInfo>>{@Overridepublic void render(Treeitem item, DefaultTreeNode<FileInfo> data, int index)throws Exception {// TODO Auto-generated method stub item.setValue(data);final FileInfo fi = data.getData();item.setOpen(false);//for update treeNode dataTreerow tr = item.getTreerow();if(tr == null) {tr = new Treerow();} else {tr.getChildren().clear();}item.appendChild(tr);//render file path cellTreecell pathCell = new Treecell();pathCell.setLabel(fi.getPath());pathCell.setParent(tr);//render file description cellTreecell descriptionCell = new Treecell();descriptionCell.setLabel(fi.getDescription());descriptionCell.setParent(tr);}}public class FileInfo {private String path;private String description;public FileInfo() {}public FileInfo(String path, String description) {this.path = path;this.description = description;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}}
参考:
http://zkfiddle.org/sample/21g0stt/2-Another-new-ZK-fiddle#source-1