您好:) iam bootcamp java仍然了解有关java和框架zk的更多信息
任何人都知道如何创建动态单选按钮,例如数据库中的数据 现在我没用它。
这是我测试的源代码
radiobutton.zul
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="Customer" border="normal" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('vmd.RadioButton')">
<cell>
<vlayout>
<radiogroup id="radiogroup">
</radiogroup>
</vlayout>
</cell>
</window>
</zk>
RadioButton.java
public class RadioButton {
public Radiogroup getRadiogroup() {
return radiogroup;
}
public void setRadiogroup(Radiogroup radiogroup) {
this.radiogroup = radiogroup;
}
private Radiogroup radiogroup = new Radiogroup();
@Init
public void load() {
ArrayList<String> columnEntries = new ArrayList<String>();
columnEntries.add("a");
columnEntries.add("b");
for (String entry : columnEntries) {
radiogroup.appendItem(entry, entry);
}
radiogroup.setVisible(true);
}
}
该代码的结果是没有单选按钮而且没有错误:*(请帮帮我..
答案 0 :(得分:3)
根据ZK组件生命周期,在组件呈现之前调用init
方法。所以你必须在撰写后设置值(@AfterCompose
)。您还可以wire您的组件。示例:
Viewmodel :
package your.package;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Radiogroup;
public class RadioButton {
@Wire
private Radiogroup radiogroup;
@AfterCompose
public void init(@ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
radiogroup.appendItem("Test 1", "test1");
radiogroup.appendItem("Test 2", "test2");
}
}
查看:
<vlayout apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('your.package.RadioButton')">
<radiogroup id="radiogroup"/>
</vlayout>
您还可以从viewmodel绑定列表。
示例:
Viewmodel :
private List<String> optionList;
private String selectedOption;
public String getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
public List<String> getOptionList() {
return optionList;
}
public void setOptionList(List<String> optionList) {
this.optionList = optionList;
}
@Init
public void init() {
List<String> entries = new ArrayList<String>();
entries.add("a");
entries.add("b");
setOptionList(entries);
}
查看:
<vlayout apply="org.zkoss.bind.BindComposer" viewModel="@id('vmd') @init('your.package.yourViewModel')">
<radiogroup selectedItem="@bind(vmd.selectedOption)" model="@load(vmd.optionList)">
<template name="model">
<radio label="@load(each)" value="@load(each)"/>
</template>
</radiogroup>
</vlayout>
更新:正如Malte Hartwig在评论中所述,使用MVVM时首选这种方式(不建议使用组件布线,请参阅http://books.zkoss.org/zk-mvvm-book/8.0/advanced/wire_components.html)。
还有其他几种方法,例如使用Composer。