ZK

时间:2017-07-04 16:54:56

标签: zk dynamically-generated zul

我是ZK的全新人物。我需要动态创建带有标签的N组合框并填充它们。我已经用它的id填充了一个组合框,但由于可能有很多组合框我不应该知道它们的ID,所以它不能解决我的问题。

我需要添加N个组合框,它们的标签并动态填充它们。有没有办法创建这组组合框并动态设置它们?有什么想法吗?

下面的代码用于填充已经知道其固定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>

1 个答案:

答案 0 :(得分:1)

此问题与您的last question非常相似。您应该将父容器(在这种情况下为hlayout)连接到控制器,然后在那里创建组件。

@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);
    }
}

如果您使用MVVM,则可以使用children binding在zul中创建组件。