代码之家  ›  专栏  ›  技术社区  ›  Luisa Hernández

如何在ZK中动态添加listheaders和listcells

  •  0
  • Luisa Hernández  · 技术社区  · 7 年前

    列表头和 N

    问题可能是这样的:

       @Wire
       private Window idWindow;
    
       private Listheader header;
       private Listcell item1;
    
      @Override
        public void onCreate(Event event) {
    
        header.setLabel("laaaa");// It would set just one header but I can have many (N headers) and same for items
    
       }
    
    <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 -->
    
       <listbox id="mainList" hflex="1" vflex="1">
          <listhead>
              <listheader id="header" label="A" />
              <listheader id="header1" label="B"  /> 
              <listheader id="header2" label="C"  />
              ....
              <listheader id="headerN" label="N" />             
          </listhead>
          <listitem>
              <listcell id="item1" label="A"/>
              <listcell id="item2" label="B"/>
              <listcell id="item3" label="C"/>
              ....
              <listcell id="itemN" label="D"/>
          </listitem>
         </listbox>
    
       <!-- CONTINUES -->
        </window>
    </zk>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Malte Hartwig Darren Gunning    7 年前

    你可以离开 listhead 在zul中清空,将其连接到控制器并创建 listheaders 那里重要的一步是询问 listbox ,并附加 对它。对于单元格,请给出您的 列表框

    <zk>
        <window ... >
            <listbox id="mainList" hflex="1" vflex="1">
                <listhead />
            </listbox>
        </window>
    </zk>
    

    然后在控制器中,在 doAfterCompose 并附加渲染器:

    @Wire
    private Listbox mainList;
    
    @Override  // This method should be specified by a composer super class
    public void doAfterCompose(Component comp)throws Exception
    {
        super.doAfterCompose(comp);
    
        mainList.setModel(someModelWithYourData);
    
        // create listheaders (manually/in for-loop/based on data...)
        Listhead head = mainList.getListhead();
        head.appendChild(new Listheader("A"));
        ...
    
        // attach renderer
        mainList.setItemRenderer(new ListitemRenderer<Object>() // use proper data type instead of Object
        {
            @Override
            public void render(Listitem item, Object data, int index)
                throws Exception
            {
                item.appendChild(new Listcell("a"));
                ...
            }
        });
    }
    

    zk的开发者网站上也有一个例子: https://www.zkoss.org/wiki/ZK_Developer%27s_Reference/MVC/View/Renderer/Listbox_Renderer

    listitems 在zul或控制器中,然后创建列表单元格:

    for (Component child : mainList.getChildren()) 
    {
        if (child instanceof Listitem) 
        {
            Listitem item = (Listitem) child;
            // do the same as in the renderer
        }
    }