-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I Have a question about using template in MVC. In case of Listbox is the template meant only for displaying data or is it like a proper data binding? All examples and documentation I have found are only using components like Label and nothing that can modify data like Textbox for example.
I have tried to simply add setter methods but it didn't work for me. Could you please provide some example?
Or alternatively I'm pasting my example which is NOT working:
View:
<window title="Data Binding in MVC" border="normal" apply="MyComposer"> <button id="changeButton" label="Change data"></button> <listbox id="listbox" model="${$composer.dataModel}"> <listhead> <listheader>Name</listheader> <listheader>Info</listheader> </listhead> <template name="model"> <listitem> <listcell> <label value="${each.name}"></label> </listcell> <listcell> <textbox value="${each.info}"></textbox> </listcell> </listitem> </template> </listbox> </window>
Composer:
public final class MyComposer extends SelectorComposer<Component> { @Wire private Button changeButton; @Wire private Listbox listbox; private ListModelList<Person> dataModel = new ListModelList<Person>(); public ListModelList<Person> getDataModel() { return dataModel; } public void setDataModel(ListModelList<Person> newDataModel) { dataModel = newDataModel; } @Override public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); for (int i = 0; i < 10; i++) { dataModel.add(new Person()); } } @Listen("onClick = #changeButton") public void onButtonClick() { for (int i = 0; i < dataModel.getSize(); i++) { dataModel.getElementAt(i).setName("New Name"); dataModel.getElementAt(i).setInfo("New Info"); } Clients.showNotification("Data has been changed"); } }
public class Person { private String name; private String info; public String getName() { return name; } public void setName(String newName) { name = newName; } public String getInfo() { return info; } public void setInfo(String newInfo) { info = newInfo; } public Person() { name = "Initial Name"; info = "Initial Info"; } }
Hi jj thanks for your help but it doesn't work either. First I'm adding "org.zkoss.zkplus.databind.AnnotateDataBindingComposer" to window's apply attribute. Then I'm changing all $ to @. As a result I'm getting empty Listbox with 10 rows, so the size of data model is correct but values are not set in the components, all Labels and Textboxes are empty. Plus the same problems as with $.
Asked: 2012-09-09 09:51:04 +0800
Seen: 99 times
Last updated: Sep 10 '12