-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I am attempting to create a header on a grid programatically as would be done for a Richlet (ie not using zul file)
There are many examples with zul files found at http://www.zkoss.org/zkdemo/grid but none done in java api.
I have attempted the following. What is not clear is when in the process of creating a grid should headers (or column labels) be added. (and how).
Grid grid = new Grid(); ListModelMap m = new ListModelMap(someHashMap) grid.setModel(m); grid.setRowRenderer(new CustomGridRowRenderer()); Auxhead ah = new Auxhead(); Auxheader ah1 = new Auxheader("Header 1"); ah1.setParent(ah); grid.insertBefore(ah, grid.getColumns()); // done header grid.setParent(someParent);
thank you for your help
Hi kbsimm,
It shouldn't matter as long as it is done in the same Execution. Are you getting some error/exception? However I will not recommend this approach. Better set all the grid/and its children attributes first and then set model and renderer. Much easier to read and maintain.
I am not getting any error but I am not seeing the header either. I tried adding the header before the model first but that didn't work. And in fact that doesn't make sense to me as I think that before the model is applied the grid would not have any columns for the insertBefore call.
It also is not clear where I am putting this header. Ie placing it with an insertBefore method seems a little arbitrary. If grids have headers (there is a getHead method) then why not just setHeader(header).
Also any specific reason why you aren't just using Columns/Column like below?
Columns cols = new Columns(); Column col1 = new Column("First"); col1.setParent(cols); Column col2 = new Column("Last"); col2.setParent(cols); cols.setParent(g); // g is grid
1) I don't understand how grid.appendChild(ah) or ah.setParent(grid) would work. Doesn't a header have special meaning to a grid. How does a set parent know it's a grid vs some other component.
2) Doesn't the use of a model preclude the creation of specific columns. Columns will be created based on the model.
Thank you so much for your help. It's seems most people here write in .zul so help in GenerucRichlet and ZK java API is scarce.
Hi kbsimm,
From Auxheader component reference "Auxiliary headers should be accompanied with columns/listhead/treecols when used with grid/listbox/tree." What I understand from this is that you need to explicitely tie Columns component with Auxhead/auxheader so that ZK can render th html elements correctly. so doing this worked for me
Auxhead ah = new Auxhead(); Auxheader ah1 = new Auxheader("Header 1"); ah1.setColspan(2); ah1.setParent(ah); Columns cols = new Columns(); cols.setParent(g); ah.setParent(g);
Hope this solves your issue.
Here is a working example just in case
package j3lj7ksf$v1;import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;
import java.util.ArrayList;public class TestComposer extends GenericForwardComposer{
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
Grid g = new Grid();
g.setId("testgrid");
g.setRowRenderer(new RowRenderer<Person>() {public void render(Row row, Person data, int arg2)
throws Exception {
Cell c1 = new Cell();
c1.appendChild(new Label(data.getFirstname()));
c1.setParent(row);
Cell c2 = new Cell();
c2.appendChild(new Label(data.getLastname()));
c2.setParent(row);
}
});
ArrayList<Person> m = new ArrayList<Person>();
m.add(new Person("A1", "D1"));
m.add(new Person("A2", "D2"));
m.add(new Person("A3", "D3"));
m.add(new Person("A4", "D4"));
m.add(new Person("A5", "D5"));
g.setModel(new ListModelList<Person>(m));
g.setParent(comp);
Auxhead ah = new Auxhead();
Auxheader ah1 = new Auxheader("Header 1");
ah1.setColspan(2);
ah1.setParent(ah);
// need empty Columns component for Auxhead
Columns cols = new Columns();
cols.setParent(g);
ah.setParent(g);
}
class Person {
String firstname;
String lastname;
public Person(String firstname, String lastname) {
super();
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
}
<zk>
<window border="normal" title="hello" apply="j3lj7ksf$v1.TestComposer">
</window>
</zk>
Thank you ashishd all is working now but it doesn't make any sense to me.
In your one of your previous replies you said
From Auxheader component reference "Auxiliary headers should be accompanied with columns/listhead/treecols when used with grid/listbox/tree." What I understand from this is that you need to explicitely tie Columns component with Auxhead/auxheader so that ZK can render th html elements correctly. so doing this worked for me Auxhead ah = new Auxhead(); Auxheader ah1 = new Auxheader("Header 1"); ah1.setColspan(2); ah1.setParent(ah); Columns cols = new Columns(); cols.setParent(g); ah.setParent(g);
There are a couple of things that just don't make sense and seem to be weaknesses in ZK.
1) It doesn't seem that the above has any relationship to the model which doesn't make sense to me. The model establishes the content and columns (or so I would presume) and thus a creating new columns and adding them to the grid doesn't make sense. I would expect to do this with grid.getColumns not new Columns().
2) This component.setParent(component) establishment of a relationship is to week for my liking. Is the implementation forcing set parent to do a bunch of "instanceOf" checks?
Any ZK developers that can explain this?
kbsimm,
Regarding your concerns:
1. Columns don't have any direct relationship to model. It is purely a UI construct. You can have 10 items per row in your model, but only render 5 columns. It all depends on your rowrenderer implementation. There is clearly a separation between model and view here.
2. There is probably something like that, though I didn't read ZK code to know for sure. This is a generic relationship apply to all components. There could be more specific methods(e.g. columns.setGrid(grid), or grid.setColumns(cols)), which I consider as syntactic sugar. I don't see it as weak, because it will give you runtime exceptions if you are adding wrong things to a component as children.
Asked: 2012-10-02 22:47:03 +0800
Seen: 264 times
Last updated: Oct 09 '12