-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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??
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
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?
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))); }
we use Executions.createComponents("items.zul", row, params) and forEach
it will prepare a zul page and just need to attach one time
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.
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.
Hi yianpuodiaotu
Could you provide a sample for reproduce this problem?
what version of ZK are you use?
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.
no problem
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
Asked: 2010-02-09 08:33:38 +0800
Seen: 10,702 times
Last updated: Nov 15 '12