zkdynamically-generatedzul

Group of combobox dynamically in ZK


I am totally new in ZK. I need to create N combobox with their labels dynamically and populate them. I already populate a combobox with its id, but as there can be many combobox I should not know their ids, so It does not solve my problem.

I need to add N combobox, their labels and populate them dynamically. Is there any way to create that group of combobox and set them dynamically? Any ideas?

The code bellow works to populate the combo already knowing its fixed id.

//In this example I assume I have a label and a combobox. But could have 0 to N of them.

private Label lblComboMetadatos;
private Combobox cmbMetadatos;

//THEN

if (cmbMetadatos.getItemCount() == 0) {
  lblComboMetadatos.setValue(trdMetaTipoDocumental.getNombreMetadato()); //Here I set the name of label but I should really can not know how many of them could be. There may exist 0..N 
  for (TrdMetadato trdMetaDato: trdMetaTipoDocumental.getTrdMetadatos()) {

    String enumValores = trdMetaDato.getValoresEnumerado(); //Here I set the values of a combobox but I can not know how many of them could be. There may exist 0..N  
    cmbMetadatos.appendItem(enumValores]);
}

}
<zk>
  <window id="idWindow" title="nameWindow" apply="controller.java" border="normal" closable="true" sizable="true" maximizable="true" maximized="true" height="85%" width="150%" style="overflow:auto;">
    <!-- CONTINUES -->
    <groupbox>
      <hlayout>
        <label id="lblComboMetadatos" />
        <combobox id="cmbMetadatos"></combobox>
      </hlayout>
    </groupbox>
    <!-- CONTINUES -->
  </window>
</zk>

Solution

  • This question is very similar to your last question. You should wire the parent container (hlayout in this case) to your controller and then create the components there.

    @Wire
    private Component container; // your hlayout
    
    @Override  // This method should be specified by a composer super class
    public void doAfterCompose(Component comp) throws Exception
        for (<count an index or loop over data>) {
            hlayout.appendChild(new Label("Hello World");
            Combobox cb = new Combobox();
            // append Comboitems
            cb.addEventListener(Events.ON_SELECT, ...);
            hlayout.appendChild(cb);
        }
    }
    

    If you used MVVM, you could use children binding to create the components in zul.