Search code examples
jsfselectonemenuvaluechangelistener

valueChangeListener is invoked on submit without changing options in selectOneMenu


In my page I have selectonemenu and I have noticed that when I click the pagination of datatable, valueChangeListener method is getting invoked without actually changing values or options in selectonemenu.

How is this happening and how could I prevent this from happening as I need to invoke valueChangeListener method only if user changes values in selectonemenu.

Thanks and appreciate any help.

Regards

Here is my code for selectonemenu

Update1

<ice:selectOneMenu value="#{list.selectedItem}"
            partialSubmit="true" valueChangeListener="#{list.val}">
                <f:selectItems value="#{list.selectItems}" />
                </ice:selectOneMenu>

And bean method

   public void val(ValueChangeEvent event) {
            logger.info("1");
            selectedValue = event.getNewValue().toString();
}

Values are added like

public void loadItems(){
        selectItems.add(new SelectItem("1", "One"));
        selectItems.add(new SelectItem("2", "Two"));
        selectItems.add(new SelectItem("3", "Three"));
        selectItems.add(new SelectItem("4", "Four")); 

}

And method is called as follows

 public List<SelectItem> getSelectItems() {
        if (selectItems == null) {
            loadItems;

        }
        return selectItems;
    }

Solution

  • The JSF valueChangeListener is in no way related to the client-side events. It is a server-side event which will be invoked when the submitted value differs from the initial value.

    The first item of your list has a value of 1, so when the user doesn't change the dropdown, the submitted value will be 1. If you don't want the valueChangeListener to be invoked, then you need to ensure that the initial value also equals to 1. That is the value behind #{list.selectedItem}". Initialize it to the same value as the first item during bean's construction/initialization.

    this.selectedItem = "1";