0

Combobox binding doesn't work

asked 2013-02-26 14:46:40 +0800

maximeroussel gravatar image maximeroussel
11 1

Combobox seems to not work properly with data binding. The attribute selectedItem seems to only work one way: when the user select an item, the value is updated for the form and the VM. When I pass the bottle in parameter and I update the selectedBottle in afterCompose method, the combobox cbSelectedProduct is empty. But, the value fx.product is correct if I put a label to display it. I tried without form and the behiavoir is the same. I use hibernate and Bottle is ManyToOne relation.

What did I wrong?

Thanks a lot

My view

<zk>
<window id="bottleForm" apply="org.zkoss.bind.BindComposer"
    title="Add bottle"
    viewModel="@id('vm')@init('net.maximeroussel.vetDrugsInventory.vm.BottleFormVM')"
    form="@id('fx') @load(vm.selectedBottle) @save(vm.selectedBottle, before='save')" 
    validationMessages="@id('vmsgs')" width="350px" closable="true">
    <grid>
        <columns>
            <column hflex="min"/>
            <column hflex="2"/>
        </columns>
        <rows>
            <row>
                <label value="Product"></label>
                <combobox id="cbSelectedProduct" model="@load(vm.availableProducts)" 
                    selectedItem="@bind(fx.product)" readonly="true" focus="true">
                    <template name="model">
                        <comboitem label="@load(each.name)" value="@bind(each)"/>
                    </template>
                </combobox>
            </row>
            <row>
                <label value="Capacity"></label>
                <doublebox value="@bind(fx.capacity)" constraint="no empty"/>
            </row>
            <row>
                <label value="Expiration date"></label>
                <datebox value="@bind(fx.expirationDate)" constraint="no past"/>
            </row>
            <row>
                <label value="Purchase date"></label>
                <datebox value="@bind(fx.purchaseDate)" constraint="no empty,no future"/>
            </row>
        </rows>
    </grid>
    <separator height="25px" />

    <button label="Save" onClick="@command('save')"></button>
</window>

</zk>

VM

package net.maximeroussel.vetDrugsInventory.vm;

import java.util.List;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import net.maximeroussel.vetDrugs.RepositoriesHolder;
import net.maximeroussel.vetDrugsInventory.entities.Bottle;
import net.maximeroussel.vetDrugsInventory.entities.Product;

public class BottleFormVM {

    Bottle selectedBottle;

    @Wire
    Window bottleForm;

    @Wire
    Combobox cbSelectedProduct;

    Object parentVM;

    boolean editMode;   

    @AfterCompose
    public void afterCompose(   @ContextParam(ContextType.VIEW) Component view,
                                @ExecutionArgParam("bottle") Bottle bottle,
                                @ExecutionArgParam("parentVM") Object parentVM){
        Selectors.wireComponents(view, this, false);

        this.parentVM = parentVM;

        if (bottle != null) {
            selectedBottle = bottle;
            bottleForm.setTitle("Edit bottle");         
            editMode = true;
        } else {
            selectedBottle = new Bottle();
            editMode = false;
        }
    }


    @SuppressWarnings("unchecked")
    @Command
    public void save() {
        RepositoriesHolder.getInstance().getBottlesRepository().save(selectedBottle);

        if (editMode) {
            BottleFormVM.this.bottleForm.detach();
        } else {
            Messagebox.show("Success! The bottle Id is " + selectedBottle.getId(), 
                    "Message dialog", Messagebox.OK, Messagebox.INFORMATION, 
                    new org.zkoss.zk.ui.event.EventListener() {
                public void onEvent(Event evt) throws Exception {           
                    BottleFormVM.this.bottleForm.detach();

                    BindUtils.postNotifyChange(null, null, parentVM, "activeBottles");
                }
            });     
        }

    }

    public List<Product> getAvailableProducts() {       
        return RepositoriesHolder.getInstance().getProductsRepository().findActiveOrderByName();
    }

    public Bottle getSelectedBottle() {     
        return selectedBottle;
    }

    public void setSelectedBottle(Bottle selectedBottle) {
        this.selectedBottle = selectedBottle;
    }


}
delete flag offensive retag edit

3 Answers

Sort by » oldest newest most voted
3

answered 2013-02-26 17:03:30 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

1-Why you are mixing MVC and MVVM architecture here i do not think here any need to use @Wire as you already using MVVM Architecture.

2-And Selected item will only work when user will selected a item from the combobox otherwise it will be empty.

3-What you want to chive here Can you please clarify are you getting value from cmbobox in aftercompose()?

link publish delete flag offensive edit
1

answered 2013-02-28 18:23:23 +0800

maximeroussel gravatar image maximeroussel
11 1

I fix my problem by adding the value attribute:

<combobox id="cbSelectedProduct" model="@load(vm.availableProducts)" selectedItem="@bind(fx.product)" readonly="true" focus="true" value="@load(fx.product.name)">
<template name="model">
    <comboitem label="@load(each.name)" value="@bind(each)"/>
</template>

</combobox>

Thanks

link publish delete flag offensive edit
0

answered 2013-03-13 15:40:59 +0800

pasqualeleone gravatar image pasqualeleone flag of Italy
81 2

hi maximeroussel, which zk library are you using for your project? also I have a similar problem that occurs using libraries 6.5.1. I think that the use of selectedItem should be the only, for both bindings that for the display of the values​​, as it is with the use of libraries 6.0. It could be a bug? I show a very simple example that raises failure using libraries 6.5.1, while working with libraries 6.0.

zul file

<?page title="Combo" contentType="text/html;charset=UTF-8"?><zk>
<window title="Combo" border="normal" apply="org.zkoss.bind.BindComposer"
    viewModel="@id('umvm') @init('viewModel.ComboManagementVM')">
    New Content Here!
        <hbox>
            <combobox id="alarmCodeList"
                model="@load(umvm.clearCombo.alarmCodeList)"
                selectedItem="@bind(umvm.clearCombo.selectedAlarmCode) ">
                <template name="model" var="code">
                    <comboitem label="@load(code)" />
                </template>
            </combobox>
            <combobox
                model="@load(umvm.clearCombo.alarmNotificationEventList)"
                selectedItem="@bind(umvm.clearCombo.selectedAlarmNotificationEvent)">
                <template name="model" var="notifyEvent">
                    <comboitem
                        label="@load(notifyEvent)" />
                </template>
            </combobox>
            <combobox
                model="@load(umvm.clearCombo.alarmNotificationModeList)"
                selectedItem="@bind(umvm.clearCombo.selectedAlarmNotificationMode)">
                <template name="model">
                    <comboitem label="@load(each)" />
                </template>
            </combobox>
            <button label="CLEAR" onClick="@command('clear')"></button>
        </hbox>
</window></zk>

ViewModel

package viewModel;import org.zkoss.bind.annotation.Command;import org.zkoss.bind.annotation.Init;import org.zkoss.bind.annotation.NotifyChange;public class ComboManagementVM{private ClearCombo clearCombo;public ComboManagementVM (){    
}

@Init
public void init () {
    try {
        clearCombo = new ClearCombo();
        listInit();
    } catch (Throwable ex) {
    }
}

private void listInit(){
    /*alarm code*/
    clearCombo.getAlarmCodeList();
    /*alarm notification Event*/
    clearCombo.getAlarmNotificationEventList();
    /*alarm notification Mode*/
    clearCombo.getAlarmNotificationModeList();
}

@Command
@NotifyChange ({"clearCombo"})
public void clear (){
    clearCombo.setSelectedAlarmCode(null);
    clearCombo.setSelectedAlarmNotificationEvent(null);
    clearCombo.setSelectedAlarmNotificationMode(null);
}

public void setClearCombo(ClearCombo clearCombo) {
    this.clearCombo = clearCombo;
}

public ClearCombo getClearCombo() {
    return clearCombo;
} }

Model

package viewModel;import java.util.ArrayList;import java.util.List; public class ClearCombo {

private List <String> alarmCodeList;
private List <String> alarmNotificationEventList;
private List <String> alarmNotificationModeList;
private String selectedAlarmCode;
private String selectedAlarmNotificationEvent; 
private String selectedAlarmNotificationMode;

ClearCombo(){
    alarmCodeList = new ArrayList<String>();
    alarmNotificationEventList =new ArrayList<String>(); 
    alarmNotificationModeList =new ArrayList<String>();
    alarmCodeList.add("a1");
    alarmCodeList.add("a2");
    alarmCodeList.add("a3");
    alarmCodeList.add("a4");
    alarmNotificationEventList.add("b1");
    alarmNotificationEventList.add("b2");
    alarmNotificationEventList.add("b3");
    alarmNotificationEventList.add("b4");
    alarmNotificationModeList.add("c1");
    alarmNotificationModeList.add("c2");
    alarmNotificationModeList.add("c3");
    alarmNotificationModeList.add("c4");
}
public List<String> getAlarmCodeList() {
    System.out.println("alarmCode size "+alarmCodeList.size() );
    return alarmCodeList;
}
public void setAlarmCodeList(List<String> alarmCodeList) {
    this.alarmCodeList = alarmCodeList;
}
public List<String> getAlarmNotificationEventList() {
    return alarmNotificationEventList;
}
public void setAlarmNotificationEventList(
        List<String> alarmNotificationEventList) {
    this.alarmNotificationEventList = alarmNotificationEventList;
}
public List<String> getAlarmNotificationModeList() {
    return alarmNotificationModeList;
}
public void setAlarmNotificationModeList(
        List<String> alarmNotificationModeList) {
    this.alarmNotificationModeList = alarmNotificationModeList;
}
public String getSelectedAlarmCode() {
    return selectedAlarmCode;
}
public void setSelectedAlarmCode(String selectedAlarmCode) {
    this.selectedAlarmCode = selectedAlarmCode;
}
public String getSelectedAlarmNotificationEvent() {
    return selectedAlarmNotificationEvent;
}
public void setSelectedAlarmNotificationEvent(
        String selectedAlarmNotificationEvent) {
    this.selectedAlarmNotificationEvent = selectedAlarmNotificationEvent;
}
public String getSelectedAlarmNotificationMode() {
    return selectedAlarmNotificationMode;
}
public void setSelectedAlarmNotificationMode(
        String selectedAlarmNotificationMode) {
    this.selectedAlarmNotificationMode = selectedAlarmNotificationMode;
}}
link publish delete flag offensive 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
1 follower

RSS

Stats

Asked: 2013-02-26 14:46:40 +0800

Seen: 386 times

Last updated: Mar 13 '13

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