0

non uniform rowSpans in a grid produces extra column

asked 2012-09-28 17:53:02 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

updated 2012-09-28 17:57:21 +0800

Referring to a thread on Dynamic Grid Creation with rowSpan the code below shows a modification where the a random number of subsubarticles are added to the subarticles. The purpose is to change the number of rows to span in the subarticles column for each subarticle.

The problem is this produces a 4th column and the data seems to fall in the wrong rows.

Any insight on this 4th column would be appreciated.


The only code modification is in the getSubSubArticle method as shown here:

public static List getSubSubArticle(String SubArticle) {
  List list = new ArrayList();
  //////////////////////////////////////
  // only code change is here to add a random number of subarticles
  Random rg = new Random();
  int count = rg.next(6) + 1; // Add 1 as zero is not allowed
  //// done with code change
  for (int i = 0; i < count; i++)
    list.add("Sub" + SubArticle + i);
  return list;
}

Full code

<zk>
	<zscript><![CDATA[
		import java.util.*;
		
		import org.zkoss.zk.ui.Component;
		import org.zkoss.zk.ui.Executions;
		import org.zkoss.zk.ui.util.GenericForwardComposer;
		import org.zkoss.zul.*;
		
		public class MyComposer extends GenericForwardComposer {
			public void doBeforeComposeChildren(Component comp) throws Exception {
				super.doBeforeComposeChildren(comp);
				
				comp.setAttribute("model", createModel());
				comp.setAttribute("renderer", new MyRowRenderer());
			}
		
			
			private ListModel createModel() {
				List data = new ArrayList();
				
				List articleList = MyDAO.getArticle();
				for (int i = 0, j = articleList.size(); i < j; i++) {
					String article = (String) articleList.get(i);
					List subArticleList =  MyDAO.getSubArticle(article);
					
					for (int k = 0, l = subArticleList.size(); k < l; k++) {
						String subArticle = (String) subArticleList.get(k);
						List subSubArticleList =  MyDAO.getSubSubArticle(subArticle);
						
						for (int m = 0, n = subSubArticleList.size(); m < n; m++) {
							String subSubArticle = (String) subSubArticleList.get(m);
							data.add(new String[]{article, subArticle, subSubArticle});
						}
					}
				}
				return new ListModelList(data);
			}
		
		
			public static class MyDAO {
				public static List getArticle() {
					List list = new ArrayList();
					for (int i = 0; i < 5; i++)
						list.add("Article " + i);
					return list;
				}
				public static List getSubArticle(String article) {
					List list = new ArrayList();
					for (int i = 0; i < 5; i++)
						list.add("Sub" + article + i);
					return list;
				}
				public static List getSubSubArticle(String SubArticle) {
					List list = new ArrayList();
					//////////////////////////////////////
					// only code change is here to add a random number of subarticles
					Random rg = new Random();
					int count = rg.next(6) + 1; // Add 1 as zero is not allowed
					//// done with code change
					for (int i = 0; i < count; i++)
						list.add("Sub" + SubArticle + i);
					return list;
				}
				
				public static int getTotalArticleCount(String article) {
					List subArticleList =  getSubArticle(article);
					int rowSpan = 0;
					for (int i = 0, j = subArticleList.size(); i < j; i++) {
						String subArticle = (String) subArticleList.get(i);
						rowSpan += getSubSubArticle(subArticle).size();
					}
					return rowSpan;
				}
			}
			
			public static class MyRowRenderer implements RowRendererExt, RowRenderer {
				private int index = 0;
				private String prevArticle = "", prevSubArticle="";
				private String[] curData;
				
				public Row newRow(Grid grid) {
					Row row = new Row();
					curData = getData(grid.getModel());
					boolean isNextArticle = !prevArticle.equals(curData[0]);
					boolean isNextSubArticle = !prevSubArticle.equals(curData[1]);
					
					Map arg = new HashMap();
					
					// first row of each SubArticle
					if (isNextSubArticle) {
						
						// first row of each article
						if (isNextArticle) {
							Cell cell = new Cell();
							prevArticle = curData[0];
							cell.setRowspan(MyDAO.getTotalArticleCount(prevArticle));
							cell.appendChild(new Label(prevArticle));
							row.appendChild(cell);
						}
						
						prevSubArticle = curData[1];
						Cell cell = new Cell();
						cell.setRowspan(MyDAO.getSubSubArticle(prevSubArticle).size());
						cell.appendChild(new Label(prevSubArticle));
						row.appendChild(cell);
					}
					return row;
				}
				private String[] getData(ListModel model) {
					if (model != null)
						return (String[]) model.getElementAt(index++);
					return null;
				}
				public Component newCell(Row row) {
					return new Label(curData[2].toString());
				}
				public int getControls() {
					return 0;
				}
				public void render(Row row, Object data) throws Exception {
				}
			}
		}
	]]></zscript>
	<window apply="MyComposer">
		<grid rowRenderer="${renderer}" model="${model}">
			<columns>
				<column label="Article"/>
				<column label="SubArticle"/>
				<column label="SubArticle"/>
			</columns>
		</grid>
	</window>
</zk>

thank you

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2012-09-29 01:13:29 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

Resolved.....

Below is the code modified to use a couple of hashMaps to keep track of the count articles created. These numbers are then used in setRowSpan instead of recreating the article lists.

<zk>
	<zscript><![CDATA[
		import java.util.*;
		
		import org.zkoss.zk.ui.Component;
		import org.zkoss.zk.ui.Executions;
		import org.zkoss.zk.ui.util.GenericForwardComposer;
		import org.zkoss.zul.*;
		
		import java.util.logging.Level;
		import java.util.logging.Logger;
		
		public class MyComposer extends GenericForwardComposer {
			public void doBeforeComposeChildren(Component comp) throws Exception {
				super.doBeforeComposeChildren(comp);
				
				comp.setAttribute("model", createModel());
				comp.setAttribute("renderer", new MyRowRenderer());
			}
			
			static Logger theLog = Logger.getLogger("com.ArivX.SRP");
		
			static HashMap AHash = new HashMap();
			static HashMap SAHash = new HashMap();
			Integer ACount = new Integer(0);
			
			private ListModel createModel() {
				List data = new ArrayList();
				
				List articleList = MyDAO.getArticle();
				for (int i = 0, j = articleList.size(); i < j; i++) {
					String article = (String) articleList.get(i);
					List subArticleList =  MyDAO.getSubArticle(article);
					
					for (int k = 0, l = subArticleList.size(); k < l; k++) {
						String subArticle = (String) subArticleList.get(k);
						List subSubArticleList =  MyDAO.getSubSubArticle(subArticle);
						
						for (int m = 0, n = subSubArticleList.size(); m < n; m++) {
							String subSubArticle = (String) subSubArticleList.get(m);
							data.add(new String[]{article, subArticle, subSubArticle});
							
							ACount++;
						}
						SAHash.put(subArticle, subSubArticleList.size());
					}
					AHash.put(article, ACount);
					ACount = new Integer(0);
				}
				return new ListModelList(data);
			}
		
		
			public static class MyDAO {
				public static List getArticle() {
					List list = new ArrayList();
					for (int i = 0; i < 2; i++)
						list.add("Article " + i);
					return list;
				}
				public static List getSubArticle(String article) {
					List list = new ArrayList();
					for (int i = 0; i < 3; i++)
						list.add("Sub" + article + i);
					return list;
				}
				public static List getSubSubArticle(String SubArticle) {
					List list = new ArrayList();
					//////////////////////////////////////
					// only code change is hear to add a random number of subarticles
					Random rg = new Random();
					int count = rg.nextInt(6) + 1; // Add 1 as zero is not allowed
					//int count = 5;
					//// done with code change
					for (int i = 0; i < count; i++)
						list.add("Sub" + SubArticle + i);
					return list;
				}
				
				public static int getTotalArticleCount(String article) {
					List subArticleList =  getSubArticle(article);
					int rowSpan = 0;
					for (int i = 0, j = subArticleList.size(); i < j; i++) {
						String subArticle = (String) subArticleList.get(i);
						rowSpan += getSubSubArticle(subArticle).size();
						//rowSpan += subArticle.length();
					}
					return rowSpan;
				}
			}
			
			public static class MyRowRenderer implements RowRendererExt, RowRenderer {
				private int index = 0;
				private String prevArticle = "", prevSubArticle="";
				private String[] curData;
				
				public Row newRow(Grid grid) {
					Row row = new Row();
					curData = getData(grid.getModel());
					boolean isNextArticle = !prevArticle.equals(curData[0]);
					boolean isNextSubArticle = !prevSubArticle.equals(curData[1]);
					
					Map arg = new HashMap();
					
					// first row of each SubArticle
					if (isNextSubArticle) {
						
						// first row of each article
						if (isNextArticle) {
							Cell cell = new Cell();
							prevArticle = curData[0];
							//cell.setRowspan(MyDAO.getTotalArticleCount(prevArticle));
							cell.setRowspan((Integer) AHash.get(prevArticle));
							theLog.info(prevArticle + " count is " + AHash.get(prevArticle));
							cell.appendChild(new Label(prevArticle));
							row.appendChild(cell);
						}
						
						prevSubArticle = curData[1];
						Cell cell = new Cell();
						cell.setRowspan(MyDAO.getSubSubArticle(prevSubArticle).size());
						cell.setRowspan((Integer) SAHash.get(prevSubArticle));
					    theLog.info(prevSubArticle + " count is " + SAHash.get(prevSubArticle));
						cell.appendChild(new Label(prevSubArticle));
						row.appendChild(cell);
					}
					return row;
				}
				private String[] getData(ListModel model) {
					if (model != null)
						return (String[]) model.getElementAt(index++);
					return null;
				}
				public Component newCell(Row row) {
					return new Label(curData[2].toString());
				}
				public int getControls() {
					return 0;
				}
				public void render(Row row, Object data) throws Exception {
				}
			}
		}
	]]></zscript>
	<window apply="MyComposer">
		<grid rowRenderer="${renderer}" model="${model}">
			<columns>
				<column label="Article"/>
				<column label="SubArticle"/>
				<column label="SubArticle"/>
			</columns>
		</grid>
	</window>
</zk>

link publish delete flag offensive edit

answered 2012-09-28 22:06:07 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

I just figured out that this example is not so good as the setRowSpan methods call for recreating the subartical data in order to count it. If random is used they won't be the same. I will rewrite the example and post a correction.

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: 2012-09-28 17:53:02 +0800

Seen: 107 times

Last updated: Sep 29 '12

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