javalistboxzkzk-grid

Zk Macrocomponent Listbox rendering


I have a macrocomponent like this.

<listbox id="results">
   <listitem id='listitem' self="@{each=d}">
        <listcell label='@{d.column1}'/>
        <listcell label='@{d.column2}'/>
        <listcell label='@{d.column3}'/>  
        <listcell label='@{d.column4}'/>
   </listitem>
</listbox>

I set set model using BindingListModelList just as a regular listbox.

The listbox is render but only the 1 column and shows the toString method of the adapter.

final List<Students>students = ...........
results.setModel(new BindingListModelList<>(students,false));

And then show the toString() method of the Student class.

I have try this syntax

<listitem id='listitem' self="@{each=${arg.includer.adapters}}">
    <listcell label='${arg.includer.adapters[self.columIndex].column1}'/>
    <listcell label='${arg.includer.adapters[self.columIndex].column2}'/>
    <listcell label='${arg.includer.adapters[self.columIndex].column3}'/>
    <listcell label='${arg.includer.adapters[self.columIndex].column4}'/>
</listitem>

And the HTMLMacroComponent class has a method getAdapters with returns the data to show.

public class StudentDetail extends HtmlMacroComponent{
public List<Students>getAdapters(){
    return adapters;
}
}

But I couldn't make it work. Sees that there is not itemRender like when you create the listbox using new Listbox() and sets not itemRender.


Solution

  • As far as I can see the problem is that your zul just add a listitem (i.e. 1, and only exactly 1 listitem), not a template that tells the listbox how to render your model. (At least from your code snippets I assume you want to use template. Let me know if I misunderstood you.)

    What your code does is: parse the zul to build the components, which creates the 1 listitem you defined there. Then it sets the model, which removes all existing listitems (i.e. the one you created in zul), and renders the model values with the itemrenderer or the provided template. As you did not provide either, it defaults to just rendering one column with toString().

    Try it like this:

    <listbox>
        <template name="model">
            <listitem>
                <listcell label="${each.column1}"/>
                <listcell label="${each.column2}"/>
                <listcell label="${each.column3}"/>  
                <listcell label="${each.column4}"/>
           </listitem>
        </template>
    </listbox>
    

    You can read about templates here: https://www.zkoss.org/wiki/ZK_Developer%27s_Reference/MVC/View/Template/Listbox_Template

    Alternatively you could use Listbox.setItemRenderer() to use a Java-based renderer instead of the template.

    Note that you had confused quite some zk things here: