【问题标题】:Navigate into Listbox zk with radio component使用 radio 组件导航到 Listbox zk
【发布时间】:2018-02-19 15:28:23
【问题描述】:

我在 ZUL 页面中有这个 Listbox

<listbox id="lstboxResult">
    <listhead sizable="true">
        <listheader label="Editore" />
        <listheader id="lstAzienda" />
    </listhead>
</listbox>

还有一个ListitemRender

public void render(Listitem listitem, Object data, int index) throws Exception {
    final String o = (String) data;
    if (!o.equals("")) {
        addListcell(listitem, String.valueOf(o));
        addRadiogroup(listitem);
    }
}

private void addListcell(Listitem listitem, String value) {
    Listcell lc = new Listcell();
    Label lb = new Label(value);

    lb.setParent(lc);
    lc.setParent(listitem);
}


private void addRadiogroup(Listitem listitem) {
    Radio radAla = new Radio("Label1");
    Radio radPeg = new Radio("Label2");
    Radio radRw = new Radio("Label3");

    Radiogroup rg = new Radiogroup();
    radAla.setRadiogroup(rg);
    radPeg.setRadiogroup(rg);
    radRw.setRadiogroup(rg);
    radAla.setParent(rg);
    radPeg.setParent(rg);
    radRw.setParent(rg);

    Listcell lc = new Listcell();
    rg.setParent(lc);
    lc.setParent(listitem);
}

如何从列表框设置列表单元格中的标签值和选中值中获取数据? 第二列包含 3 个单选,每个单选代表一个选择。我想将这些选择保存在数据库中。我需要一个 POJO 来保存每行的标签和复选框选项。

【问题讨论】:

    标签: listbox radio-button zk


    【解决方案1】:

    您可以通过在列表模型中使用一个小对象而不是String 来实现此目的。检查收音机时,您可以将选择保存在该对象中。然后您可以稍后保存。

    看这个例子:

    lstboxResult.setModel(new ListModelList<>(Arrays.asList(new MyObject("1"), new MyObject("2"))));
    
    lstboxResult.setItemRenderer(new ListitemRenderer<MyObject>()
    {
        @Override
        public void render(Listitem listitem, MyObject data, int index)
            throws Exception
        {
            Listcell lc = new Listcell(value);
            lc.setParent(listitem);
    
            Radio radAla = new Radio("Label1");
            Radio radPeg = new Radio("Label2");
            Radio radRw = new Radio("Label3");
    
            Radiogroup rg = new Radiogroup();
            radAla.setRadiogroup(rg);
            radPeg.setRadiogroup(rg);
            radRw.setRadiogroup(rg);
            radAla.setParent(rg);
            radPeg.setParent(rg);
            radRw.setParent(rg);
    
            Listcell lc = new Listcell();
            rg.setParent(lc);
            lc.setParent(listitem);
    
            rg.addEventListener(Events.ON_CHECK, e -> {
                data.setSelectedIndex(rg.getSelectedIndex());
                data.setSelectedLabel(rg.getSelectedItem() != null ? rg.getSelectedItem().getLabel() : null);
                System.out.println(data.getLabel() + " " + data.getSelectedIndex() + " " + data.getSelectedLabel());
            });
        }
    });
    
    private static class MyObject
    {
        private String label;
        private int    selectedIndex = -1;
        private String selectedLabel;
    
        public MyObject(String label) { this.label = label; }
    
        public String getLabel() { return label; }
        public int    getSelectedIndex() { return selectedIndex; }
        public void   setSelectedIndex(int selectedIndex) { this.selectedIndex = selectedIndex; }
        public String getSelectedLabel() { return selectedLabel; }
        public void   setSelectedLabel(String selectedLabel) { this.selectedLabel = selectedLabel; }
    }
    

    现在您可以使用该模型从MyObjects 中读取选定的收音机,然后对它们做任何您想做的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      相关资源
      最近更新 更多