【问题标题】:ZK Framework - How to access the onSelect method of a Combobox that is inside a Listbox generated with a render?ZK 框架 - 如何访问使用渲染生成的列表框内的组合框的 onSelect 方法?
【发布时间】:2022-10-06 16:09:35
【问题描述】:

晚上好,我告诉你我的问题:

在 ZK 框架中,我需要在也被渲染的 Listbox 中使用动态渲染的 Combobox 的 onSelect 方法。

当我选择 Combobox 选项之一时,其内容应保存在 DocumentoVinculado 类的 observaciones 变量中。但是 onSelect 不起作用!我很感激任何帮助。附代码:

.zul

<zk>
<window id=\"myWindow\" apply=\"com.curso.controller.NewFileComposer\" title=\"Help\">
        <listbox id=\"myListbox\">
            <listhead>
                <listheader label=\"NroGEBI\"></listheader>
                <listheader label=\"Observaciones\"></listheader>
            </listhead>
        </listbox>
        <label id=\"myLabel\"></label>
</window>
</zk>

作曲家/控制器

public class NewFileComposer extends BaseController {

    private Window myWindow;
    private Listbox myListbox;
    private Combobox myCombobox0;
    private Combobox myCombobox1;
    private Label myLabel;

    public void onSelect$myCombobox0() { myLabel.setValue(myCombobox0.getValue()); }

    public void onSelect$myCombobox1() { myLabel.setValue(myCombobox1.getValue()); }
    
    public void onCreate$myWindow() {
        ListModelList<DocumentoVinculado> modelo = new ListModelList<>(crearLista());
        myListbox.setModel(modelo);
        myListbox.setItemRenderer(new NewFileRender());
    }
    
    private List<DocumentoVinculado> crearLista() {
        List<DocumentoVinculado> docVinculados = new ArrayList<>();
        docVinculados.add(new DocumentoVinculado(\"123GEBI1\", \" \"));
        docVinculados.add(new DocumentoVinculado(\"123GEBI2\", \" \"));
        return docVinculados;
    }
}

使成为

public class NewFileRender implements ListitemRenderer {

    @Override
    public void render(Listitem item, Object data, int i) throws Exception {
        DocumentoVinculado docVinculado = (DocumentoVinculado) data;

        Listcell nroGebiCell = new Listcell(docVinculado.getNroGEBI());
        nroGebiCell.setParent(item);

        Listcell opcionesCell = new Listcell();
        opcionesCell.appendChild(comboboxObservaciones(i));
        item.appendChild(opcionesCell);
    }
    
    private Combobox comboboxObservaciones(int i) {
        Combobox combobox = new Combobox();
        List<String> listaDeOpciones = listaDeOpciones();
        for(String opcion : listaDeOpciones) {
            Comboitem myComboitem = new Comboitem();
            myComboitem.setLabel(opcion);
            myComboitem.setParent(combobox);
        }       
        combobox.setId(\"myCombobox\" + i);
        return combobox;
    }
    
    private List<String> listaDeOpciones() {
        List<String> opciones = new ArrayList<>();
        opciones.add(\" \");
        opciones.add(\"Opcion1\");
        opciones.add(\"Opcion2\");
        return opciones;
    }
}

感谢您的阅读。干杯!

    标签: java combobox listbox zk


    【解决方案1】:

    从您的语法来看,您使用 GenericForwardComposer 作为 BaseController 的超类。 那是对的吗?

    根据您已经完成的工作量,我建议您检查是否可以改用SelectorComposer。 它们以相同的方式工作(连接组件和监听事件),但SelectorComposer 在连接方式方面更加明确。 GenericForwardComposer 可能有点容易出错,除非您非常清楚它的生命周期。

    关于为什么在这种情况下不会触发 onSelect :

    ZK Composers 分阶段工作。一个重要的阶段是“组合”阶段,在此期间创建组件、放置事件侦听器等。

    在该阶段结束时,将发生“afterCompose”阶段。在 afterCompose 期间,会发生“神奇”的事情(例如当前作曲家中私有组件的连接,或同一类中 onSelect$myCombobox0 的自动转发)。它还将尝试重新连接在其根锚组件上触发 onCreate 之前再次丢失的东西。

    现在,如果您的组合框是在该步骤之后动态创建的(例如,在上述所有操作完成后发生的“onCreate$myWindow”事件期间),则作曲家将已经完成所有连接和转发,并且不会知道重新检查组件是否有额外的接线。

    解释完所有这些后,您能做些什么呢?

    首先,您可以考虑将 onCreate 代码移至 doAfterCompose 方法。 如果您在 doAfterCompose(从 genericFowardComposer 覆盖)期间执行此操作,而不是等待 onCreate 生成列表框内容,您应该足够早,以便在这些组件上触发自动装配。

    这应该是这样的:

    @Override
    public void doAfterCompose(Component comp) {
        ListModelList<DocumentoVinculado> modelo = new ListModelList<>(crearLista());
        myListbox.setModel(modelo);
        myListbox.setItemRenderer(new NewFileRender());
    }
    

    第二,如果您从 genericForwardComposer 移动到 SelectorComposer,您实际上可以使用 Selectors.wireComponents 方法告诉作曲家“按需”重新连接自己。当然,这仅在您的应用程序可以针对此更改进行重构时才有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多