0

Tree structure representation

asked 2010-04-05 00:56:55 +0800

techvts gravatar image techvts
120 2 5

Is there any solution to display database table in tree structure format...

table1=prodcat_main
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| category_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category_desc | varchar(100) | YES | | NULL | |
| isparent | varchar(1) | NO | | N | |
| parentcat_id | int(10) unsigned | NO | MUL | | |
+---------------+------------------+------+-----+---------+----------------+


table 2=catparentlist
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| catparentlist_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category_id | int(10) unsigned | NO | MUL | | |
| Level | int(10) unsigned | NO | | | |
| parentcat_id | int(10) unsigned | NO | MUL | | |
+------------------+------------------+------+-----+---------+----------------+


i want to display tree structure by usinf this two tables
plz help
thanks in advance

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2010-04-06 20:08:16 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi techvts
Is it you want structure ?

catparentlist_id   |  category_id  |  Level 
        category_id  |  category_desc 
        category_id  |  category_desc 
        category_id  |  category_desc 
catparentlist_id   |  category_id  |  Level 
        category_id  |  category_desc 
        category_id  |  category_desc 

and what column are you want to show?

link publish delete flag offensive edit

answered 2010-04-08 00:05:42 +0800

techvts gravatar image techvts
120 2 5

yes

link publish delete flag offensive edit

answered 2010-04-09 18:40:36 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

updated 2010-04-09 18:42:39 +0800

Ok, maybe the structure before I mention is wrong
I thank it shall be like below, because the category_id must unique

category_id  |  category_desc 
        catparentlist_id   |  category_id  |  Level 
        catparentlist_id   |  category_id  |  Level 
        catparentlist_id   |  category_id  |  Level 
category_id  |  category_desc 
        catparentlist_id   |  category_id  |  Level 
        catparentlist_id   |  category_id  |  Level 


anssume to use JPA
it will mapping to a bean

index.zul
<zk>
	<window apply="ctrl.Composer1">
		<tree id="tree"/>
	</window>
</zk>


Composer1.java
package ctrl;

import java.util.*;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.*;

public class Composer1 extends GenericForwardComposer{
	
	private Tree tree;
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		tree.setTreeitemRenderer(new TreeitemRenderer() {			
			@Override
			public void render(Treeitem item, Object data) throws Exception {
				if (data == null) return;
				
				SimpleTreeNode t = (SimpleTreeNode)data;
				
				Object obj = t.getData();
				
				if (obj instanceof Prodcat){
					createProdcatRow(item, (Prodcat)obj);
				} else if(obj instanceof Catparentlist){
					createCatparentlistRow(item, (Catparentlist)obj);
				}else return;
			}		
		});
		
		
		
		List<Prodcat> data = initDummyData();
		
		List<SimpleTreeNode> prodList = new ArrayList<SimpleTreeNode>();
		
		for (Prodcat producat : data) {
			List<SimpleTreeNode> cplList = new ArrayList<SimpleTreeNode>();
			for (Catparentlist cpl : producat.getCatparentlist()) {
				cplList.add(new SimpleTreeNode(cpl,new ArrayList()));
			}
			prodList.add(new SimpleTreeNode(producat,cplList));
		}		
		
		tree.setModel(new SimpleTreeModel(new SimpleTreeNode("ROOT",prodList)));
	}
	
	private void createProdcatRow(Treeitem item, Prodcat prod) {
		Treerow tr = new Treerow();
		tr.setParent(item);
		tr.appendChild(new Treecell(String.valueOf(prod.getCategoryId())));
		tr.appendChild(new Treecell(String.valueOf(prod.getCategoryDesc())));
		
	}
	
	private void createCatparentlistRow(Treeitem item, Catparentlist cpl) {
		Treerow tr = new Treerow();
		tr.setParent(item);
		tr.appendChild(new Treecell(String.valueOf(cpl.getCatparentlistId())));
		tr.appendChild(new Treecell(String.valueOf(cpl.getCategory())));
		tr.appendChild(new Treecell(String.valueOf(cpl.getLevel())));		
	}
	
	private List<Prodcat> initDummyData() {
		List<Catparentlist> list1 = new ArrayList<Catparentlist>();
		list1.add(new Catparentlist(1,1,10));
		list1.add(new Catparentlist(2,1,20));
		list1.add(new Catparentlist(3,1,30));
		
		Prodcat p1 = new Prodcat(1, "car", list1);
		
		List<Catparentlist> list2 = new ArrayList<Catparentlist>();
		list2.add(new Catparentlist(4,2,40));
		list2.add(new Catparentlist(5,2,50));		
		
		Prodcat p2 = new Prodcat(2, "motorcycle", list2);
		
		List<Prodcat> result = new ArrayList<Prodcat>();
		result.add(p1);
		result.add(p2);
		
		return result;
	}


	public class Prodcat {
		private int categoryId;
		private String categoryDesc;
		private List<Catparentlist> catparentlist;
		
	
		public Prodcat(int categoryId, String categoryDesc, List<Catparentlist> catparentlist) {
			super();
			this.categoryId = categoryId;
			this.categoryDesc = categoryDesc;
			this.catparentlist = catparentlist;
		}
		public void setCategoryId(int categoryId) {
			this.categoryId = categoryId;
		}
		public int getCategoryId() {
			return categoryId;
		}
		public void setCategoryDesc(String categoryDesc) {
			this.categoryDesc = categoryDesc;
		}
		public String getCategoryDesc() {
			return categoryDesc;
		}
		public void setCatparentlist(List<Catparentlist> catparentlist) {
			this.catparentlist = catparentlist;
		}
		public List<Catparentlist> getCatparentlist() {
			return catparentlist;
		}
	}
	
	public class Catparentlist {
		private int catparentlistId;
		private int category;
		private int level;		
		
		public Catparentlist(int catparentlistId, int category, int level) {
			super();
			this.catparentlistId = catparentlistId;
			this.category = category;
			this.setLevel(level);
		}
		public void setCatparentlistId(int catparentlistId) {
			this.catparentlistId = catparentlistId;
		}
		public int getCatparentlistId() {
			return catparentlistId;
		}
		public void setCategory(int category) {
			this.category = category;
		}
		public int getCategory() {
			return category;
		}
		public void setLevel(int level) {
			this.level = level;
		}
		public int getLevel() {
			return level;
		}		
	}

}



you can refer to here

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-04-05 00:56:55 +0800

Seen: 402 times

Last updated: Apr 09 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More