0

Dynamic grid creation

asked 2010-02-09 08:33:38 +0800

maurix gravatar image maurix
9 1 1 1

I'm usign ZK 5.0 and i want to build a grid with dynamic columns and dynamic data. I have a pojo called Page, this has a List<PageRow> fmember and every PageRow has a List<PageField> fields member. I have a zul page with a grid and in his controller class i set the model in this way:
Page page = mycode...return...

ListModelList model = new ListModelList(page.getPageRows());
mygrid.setModel(model);

in zul page i have:
<grid id="mygrid">

<rows>
<row self="@{each=item}" value="@{item}">
<custom-attributes campi="@{item.fields}" composite="list"/>
<label value="@{each}" forEach="@{campi}" ></label>
</row>
</rows>

But i have a resulting grid with all the rows but only a column with no value inside. Where i'm wrong?
It's possible draw this grid with zul page mixin with java code in controller, or i must create the grid programmatically only in java code??

delete flag offensive retag edit

35 Replies

Sort by » oldest newest

answered 2010-02-09 14:35:17 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Have you a rowRenderer in your controller??

Look at this thread . Search for the ZK Member as1225.
He posts the complete source that you can adapting to your needs.

best
Stephan

link publish delete flag offensive edit

answered 2010-02-10 04:03:34 +0800

maurix gravatar image maurix
9 1 1 1

Thank you for your reply, no i haven't a rowRenderer, so i think it's the right solution for dynamic generation of columns in every row, but for the the dynamic generations of <columns> header in my grid?

link publish delete flag offensive edit

answered 2010-02-10 20:32:23 +0800

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

updated 2010-02-10 20:32:37 +0800

Hi, maurix
I write a sample

index.zul

<zk>			
	<window apply="ctrl.MyComposer">
		<button id="btn1" label="2"/>
		<button id="btn2" label="4"/>
		<button id="btn3" label="6"/>
		<grid id="grid">
			<columns/>
		</grid>
	</window>
</zk>

MyComposer.java

package ctrl;

import java.util.*;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.*;

public class MyComposer extends GenericForwardComposer {

	private Button btn1, btn2, btn3;
	private Grid grid;	
	private ListModelList lmList;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		grid.setRowRenderer(new RowRenderer() {			
			public void render(Row row, Object data) throws Exception {
				if (data == null) return;
				
				Columns columns = row.getGrid().getColumns();
				if (columns.getChildren() == null) return;
				
				int colCount = columns.getChildren().size();
				
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("items", createStringList(colCount, String.valueOf(data)));
				row.appendChild(Executions.createComponents("items.zul", row, params));
			}
		});				
		
		lmList = new ListModelList();
		grid.setModel(lmList);
		
		
		EventListener onClickEvt = new EventListener() {			
			public void onEvent(Event event) throws Exception {
				
				Columns columns = grid.getColumns();
				if (columns.getChildren() == null) return;
				
				columns.detach();
				
				Button btn = (Button) event.getTarget();
				int num = Integer.parseInt(btn.getLabel());
				
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("columns", createStringList(num, "column"));				
				grid.appendChild(Executions.createComponents("columns.zul", grid, params));
				
				lmList.clear();
				lmList.addAll(createStringList(num, "item"));
			}
		};
		
		btn1.addEventListener("onClick", onClickEvt);
		btn2.addEventListener("onClick", onClickEvt);
		btn3.addEventListener("onClick", onClickEvt);
		
	}
	private List<String> createStringList(int num, String startWith){
		List<String> result = new ArrayList<String>();
		
		for (int i = 0; i < num; i++) {
			result.add(startWith + (i + 1));
		}
		return result;
	}
}

columns.zul

<columns>
	<column forEach="${arg.columns}" label="${each}" />
</columns>

items.zul

<label forEach="${arg.items}" value="${each}" />

maybe you think why don't write like this

for (int i = 0; i < colCount; i++) {
    row.appendChild(new Label(String.valueOf(data)));
}

because the grid will redraw again when the row call appendChild

we use Executions.createComponents("items.zul", row, params) and forEach
it will prepare a zul page and just need to attach one time

link publish delete flag offensive edit

answered 2010-02-11 03:07:09 +0800

maurix gravatar image maurix
9 1 1 1

Thank you as1225, it's a solution for my issue.
best,
maurix

link publish delete flag offensive edit

answered 2010-07-30 02:54:47 +0800

jiffle gravatar image jiffle
42 1

Thanks to as1225 for the really well worked example.

Just one small correction: You don't need to call 'appendChild' with the results of 'Executions.createComponents', as that call already appends to the parent item.
The extra appendChild causes the first column to be swapped to be the last.

link publish delete flag offensive edit

answered 2010-08-16 04:02:53 +0800

yianpuodiaotu gravatar image yianpuodiaotu
9

updated 2010-08-16 04:24:04 +0800

hi, everyone.
when I run theExample writed by the as1225. and I also noticed jffle's advice. but when I invoke the button onclic things, the IE is always busing, and nothing showed. Why??

Any advice is appreciated.

link publish delete flag offensive edit

answered 2010-08-16 05:52:19 +0800

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

Hi yianpuodiaotu
Could you provide a sample for reproduce this problem?
what version of ZK are you use?

link publish delete flag offensive edit

answered 2010-08-16 20:46:53 +0800

yianpuodiaotu gravatar image yianpuodiaotu
9

thank you, as1225,
your replay remained me to check the ZK's version. The version i using is: 5.0.0-FL,which I got from a zk demo. Maybe it has some bugs, or it's code is incomplete.
And i tried the version 3.6.3 and 5.0.3 from official site, the codes run very well.

Sorry for bringing you little trouble. tks again.

link publish delete flag offensive edit

answered 2010-08-16 20:48:49 +0800

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

no problem

link publish delete flag offensive edit

answered 2011-04-24 15:20:03 +0800

EznatiZK gravatar image EznatiZK
60

Hi as1225.
Pleaaaaaase am beging you to help me!
please i have 3 tables on my dataBase (Article, SubArticle,SubSubArtiicle) and i want to list them in a GRID dynamically. (each Article contains several SubArticles, and each SubArticle contains several SubArticals) ...the probleme is that i really do not understand how should i dooo...????? i must work with rowspan to have it work ...tu sum up...its out of my limits..
could you help meeeeeee??????
P.S the grid i want to have is just like the "spreadSheet" grid in here http://www.zkoss.org/zkdemo/grid/spreadsheet_functionalities;jsessionid=9CC54E2A391EF4DF121C9D178B7AE5F9.zkdemo?search=sprea

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-02-09 08:33:38 +0800

Seen: 10,702 times

Last updated: Nov 15 '12

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