0

Combobox problem

asked 2021-03-05 01:27:45 +0800

aUser gravatar image aUser
123 4

Hello,

i don't understand why these comboboxes update all together at the same time. This is an example..

VM:

package main;

import org.zkoss.bind.annotation.Init;
import org.zkoss.zul.ListModelList;

public class MultipleComboVM {
    private ListModelList<String> allDocs = new ListModelList<String>();
    private ListModelList<String> categories = new ListModelList<String>();

    @Init
    public void init() {
        for(int i=1; i<4; i++) {
            allDocs.add("doc" + i);
            categories.add("cat" + i);
        }
    }

    public ListModelList<String> getAllDocs() {
        return allDocs;
    }

    public void setAllDocs(ListModelList<String> allDocs) {
        this.allDocs = allDocs;
    }

    public ListModelList<String> getCategories() {
        return categories;
    }

    public void setCategories(ListModelList<String> categories) {
        this.categories = categories;
    }

}

ZUL page:

<?page title="" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="" border="normal" viewModel="@id('vm') @init('main.MultipleComboVM')" height="100%">
    <borderlayout style="border: 2px solid red; height: 100%; ">
            <west id="w" width="20%" border="0" splittable="true"  autoscroll="true" flex="true" collapsible="true" >
                    <div fulfill="self.onCreate">
                        <div children="@load(vm.allDocs) @template('children')">
                            <template name='children'>
                                <groupbox mold="3d">
                                <caption>
                                    <combobox model="@load(vm.categories) @template('categories')" onClick="event.stopPropagation()"
                                          selectedItem="@bind(each)">
                                    <template name="categories">
                                        <comboitem label="@load(each)" value="@load(each)"></comboitem>
                                    </template>
                                </combobox>
                                </caption>
                                <div children="@load(vm.allDocs) @template('thumbnails')" >
                                        <template name="thumbnails">
                                            <label value="@load(each)"></label>
                                        </template>
                                    </div>
                            </groupbox>
                        </template>
                    </div>
                </div>
        </west>
        </borderlayout>
</window>
</zk>

It's a really simple example but if i select an item on the first combobox, all the comboboxes update with that value, but only the first String was modified (which is the correct behaviour). It seems like @load(each) is not working properly here but I guess i'm missing something.

Can someone show me where is the problem here?

ZK Version is: 8.0.2.2

Thank you

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-03-09 10:49:59 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

updated 2021-03-09 10:57:48 +0800

EDIT: The problem seems to be related to ListModelList, if i use ArrayList this doesn't happen.

Seems you already found the cause. That's exactly the correct/expected behavior, since the ListModelList implements the Selectable interface it will keep track of the current selected item, if the same listModel instance is applied to multiple comboboxes they will all synchronize on the current selection.

So one way you already found is to use ArrayList which does not implement Selectable so the selection is not synchronized. (ZK will wrap this Arraylist into a new ListModelList for each combobox keeping the selection separate)

No need to worry this is all working fully 'normal'. Using ArrayList is a valid decision if the same items should be reused between multiple comboboxes but the selection needs to be kept separate.

I took the freedom to update your example and remove a few unnecessary things (E.g. a list of Strings doesn't need a comboitem template. ZK will just automatically do the same by default, the value will be the each object from the collection (here a string), the label will be computed via each.toString() - the same string). https://zkfiddle.org/sample/2msj324/5-Multiple-combobox-reduced

link publish delete flag offensive edit

Comments

I'll just stick to ArrayList in these situations, thank you!

aUser ( 2021-03-09 16:36:17 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2021-03-05 01:27:45 +0800

Seen: 10 times

Last updated: Mar 09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More