从 java 到 zul zk 的动态单选按钮

dynamic radio button from java to zul zk

你好:) 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)。您还可以 wire 您的组件。示例:

视图模型 :

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>

您还可以从视图模型绑定列表。

示例:

视图模型 :

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/zk-mvvm-book/8.0/advanced/wire_components.html)。


还有其他几种方法可以做到这一点,例如使用 Composer