Search code examples
mvvmlistboxzk

ZKoss issue with selectedItem of listbox


that's my code:

    <listbox id="boxFirma" multiple="true"
        visible="@load(vm.opzioneSelezionata eq 'firma' ? 'true' : 'false')"
        checkmark="true" width="400px" height="200px"
        model="@bind(vm.opzioniFirma)" 
        selectedItems="@bind(vm.pickedItemSet)">
        <template name="model" var="item"
            status="s">
            <listitem selected="@bind(item.preSelected)">
                <listcell label="@bind(item.valore)" />
            </listitem>

        </template>
    </listbox> <button label="Salva" style="margin-top:10px" disabled="@load(empty vm.pickedUser)"
onClick="@command('salvaPersonalizzazioneUtente')" />

The problem is when I push the button Salva, I get on the vm.pickedItemSet only the item that the user has just chosen, but nothing about the preselected items -> 'listitem selected="@bind(item.preSelected)" ' . So if there were 2 items preselected and one clicked by the user on the view model, I get just the one clicked, whereas I want all three. How do I fix this?


Solution

  • I think that your problem is behind the use of "preselected" property of your domain object. Without your View Model it's hard to understand what you are trying to achieve.

    Hovewer, let me try to address you:

    As an example, try this View Model ( "SignOption" is a bean with a single member valore). The "Salva" button will print out the set of selected list items.

     // a bunch of imports
    
    public class MultiSelectionVM {
    
        private String  opzioneSelezionata = "firma";
        private Set<SignOption> opzioniFirma = new HashSet<SignOption>();
        private Set<SignOption> pickedItemSet = new HashSet<SignOption>();
        private boolean pickedUser = true;
    
        @Init
        public void init(){
            SignOption opt1 = new SignOption();
            opt1.setValore("opt1");
            SignOption opt2 = new SignOption();
            opt2.setValore("opt2");
            SignOption opt3 = new SignOption();
            opt3.setValore("opt3");
    
            //Init list model
            opzioniFirma.add(opt1);
            opzioniFirma.add(opt2);
            opzioniFirma.add(opt3);
    
            //Init selected Items 
            pickedItemSet.add(opt2);
    
        }
    
        @Command
        public void salvaPersonalizzazioneUtente(){
            System.out.println(pickedItemSet);
        }
    
    
        //Getters and setter for all members
    }
    

    Hope this helps!