I need to add values to a listbox in zkoss using java - I mean dynamic values.
I have an zul like this
<window id="win_1" use="com.test.tets">
<listbox id="a">
<listhead>
<listheader label="Name" />
<listheader label="Age" />
开发者_StackOverflow社区 <listheader label="Sex" />
</listhead>
<listitem id="i">
</listitem>
</listbox>
</window>
I need to add values from mysql db to this listbox. How can I do that?
You can use Listmodel to do so, please refer to this article - Using a ListModel adn ListitemRenderer
you can use listmodel list, example :
yourListBox.setModel(new ListModelList<BeanClass>(beanDao.findAll()));
yourListBox.setItemRenderer(new ListItemRenderer<BeanClass>) {
@Override
public void render(Listitem item, BeanClass data, int index) throws Exception {
Listcell cell = new Listcell();
cell.appendChild(new Label(data.getFoo()));
item.appendChild(cell);
}
});
MVVM Approach
your zul file
<window id="win_1" use="com.test.tets"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('your view model class')">
<listbox id="a" model="@load(vm.listModel)">
<listhead>
<listheader label="Name" />
<listheader label="Age" />
<listheader label="Sex" />
</listhead>
<template name="model">
<listitem>
components that will represent a row in listbox...
</listitem>
</template>
</listbox>
</window>
your view model
public class SampleViewModel
{
private ListModelList<class that respresent row> listModel;
@Init
public void init()
{
init your model here....
}
public ListModelList<class that respresent row> getListModel{
return this.listMode;
}
public void setListModel(ListModelList<class that respresent row> listModel)
{
this.listModel=listModel;
}
}
The list model list will also take care of notification of changes in view model.
精彩评论