你好:) 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);
}
}
,该代码的结果是无线电按钮,也没有错误:*(请帮助我..
根据ZK组件生命周期,init
方法在组件渲染之前调用。因此,您必须在组合后设置值(@AfterCompose
)。您也可以连接组件。示例:
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>
update :如Malte Hartwig在评论中所述,使用MVVM时,这种方式是首选(否定了组件接线,请参见http://books.zkoss.org/zkoss.org/zk-mvvm-book-book/8.0/advanced/wire_components.html)。
还有其他几种方法,例如使用作曲家。