最近学习了点ZK相关东西,做了个简单的动态表。希望有ZK高手提供最优实现方式。
基本效果
1、table.zul
<?xml version="1.0" encoding="UTF-8"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
<window title="To do list" width="640px" border="normal">
<zscript>
add(){
Listitem li = new Listitem();
Listcell namecell=new Listcell();
namecell.setHeight("20px");
namecell.setDroppable("true");
Listcell pricell=new Listcell();
Listcell opencell=new Listcell();
namecell.addEventListener(org.zkoss.zk.ui.event.Events.ON_CLICK,new InlinePerson(namecell,new Textbox()));
pricell.addEventListener(org.zkoss.zk.ui.event.Events.ON_CLICK,new InlinePerson(pricell,new Textbox()));
Combobox combo=new Combobox();
String[] _dict = {
"abacus", "accuracy", "acuity", "adage", "afar", "after", "apple",
"home", "honest", "huge",
"information", "inner",
"sound", "spread", "student", "super",
"xeme",
"yea", "yellow",
"zebra", "zk",
};
ListModel dictModel= new SimpleListModel(_dict);
combo.setModel(dictModel);
combo.setAutodrop(true);
combo.setButtonVisible(true);
combo.setWidth("90%");
opencell.addEventListener(org.zkoss.zk.ui.event.Events.ON_CLICK,new InlinePerson(opencell,combo));
li.appendChild(new Listcell());
li.appendChild(namecell);
li.appendChild(pricell);
li.appendChild(opencell);
box.appendChild(li);
}
save(){
StringBuffer tmptvalue= new StringBuffer();
<![CDATA[
for (int j = 0; j < box.getItemCount(); j++) {
java.util.List clist=box.getItemAtIndex(j).getChildren();
for (int i = 0; i < clist.size(); i++) {
tmptvalue.append(clist.get(i).label+" ");
}
}
]]>
label1.setValue(tmptvalue.toString());
}
delete(){
box.removeItemAt(box.getSelectedIndex());
}
</zscript>
<listbox id="box" multiple="true" checkmark="true" mold="paging"
pageSize="10">
<listhead>
<listheader label="Item" width="55px" />
<listheader label="Name" sort="auto" />
<listheader label="Priority" width="150px" />
<listheader label="Opened" width="150px" />
</listhead>
</listbox>
<groupbox>
<button label="Add" width="36px" height="24px"
onClick="add()" />
<button label="Delete" width="46px" height="24px"
onClick="delete()" />
<button label="Save" width="46px" height="24px"
onClick="save()" />
</groupbox>
<label id="label1" value="" />
</window>
</zk>
2、InlinePerson.java
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
public class InlinePerson implements EventListener {
Listcell namecell;
Object oware;
String type;
Listitem li = new Listitem();
public InlinePerson(Listcell namecell, Object oware) {
this.oware = oware;
type = oware.getClass().getName();
if ("org.zkoss.zul.Textbox".equals(type)) {
Textbox textbox = (Textbox) oware;
textbox.setWidth("100%");
textbox.setVisible(false);
textbox.addEventListener(Events.ON_BLUR, this);
namecell.appendChild(textbox);
} else if ("org.zkoss.zul.Combobox".equals(type)) {
Combobox combo = (Combobox) oware;
combo.setVisible(false);
combo.addEventListener(Events.ON_BLUR, this);
namecell.appendChild(combo);
}
this.namecell = namecell;
}
public void onEvent(Event event) throws Exception {
String evt = event.getName();
if (Events.ON_CLICK.equals(evt)) {
// label.setVisible(false);
String tmp = namecell.getLabel();
if ("org.zkoss.zul.Textbox".equals(type)) {
Textbox textbox = (Textbox) oware;
textbox.setVisible(true);
textbox.setValue(tmp);
textbox.focus();
} else if ("org.zkoss.zul.Combobox".equals(type)) {
Combobox combo = (Combobox) oware;
combo.setVisible(true);
combo.setValue(tmp);
combo.focus();
}
namecell.setLabel("");
} else if (Events.ON_OK.equals(evt) || Events.ON_BLUR.equals(evt)) {
if ("org.zkoss.zul.Textbox".equals(type)) {
Textbox textbox = (Textbox) oware;
namecell.setLabel(textbox.getValue());
textbox.setVisible(false);
} else if ("org.zkoss.zul.Combobox".equals(type)) {
Combobox combo = (Combobox) oware;
namecell.setLabel(combo.getValue());
combo.setVisible(false);
}
}
}
}