简体   繁体   中英

PrimeFaces autocomplete loses value after update

I have a problem with PrimeFaces' autocomplete component. I'm using it with a pojo so I've created also a converter and until here everything is cool and works fine.

After the user selects an element though, I call a backing bean with the ajax component sending as a parameter the selected object. In the ajax component I've also set to update the autocomplete component itself and here comes the problem when it updates the field value of the autocomplete component with the java address of the selected pojo.

Eg: this is an address normalization service so should suggest an existing address when the user starts typing something. So the user starts typing in the field something like "john" and the autocomplete suggests "John Fitzgerald Kennedy, New York, New York" along with other suggestions as well. So say there are about 5 suggestions.

The suggestions are objects of type Address that have these properties: address, zip code, region, state and so on.

Until here everything is fine. In the suggestions the user sees only the suggested addresses along with their respective regions. And this is what I want.

Now the user clicks one of the suggestions, the ajax component calls what it has to call, does what it should do and then updates a few components along with the autocomplete component. Until before the update everything works fine and in the field I have the exact label of the item I selected. But I do the update because I need something else in the field.

XHTML:

<p:autoComplete id="address"
            value="${normalizationBean.address}" var="result" converter="#{normalizationBean}"
            itemLabel="${''.getClass().toString().equalsIgnoreCase(result.getClass().toString()) ? result : result.indirizzo }"
            itemValue="${result}" completeMethod="#{normalizationBean.loadSuggestions}"
            disabled="#{disabled}" style="width:100%;" styleClass="input indi" immediate="true">
            <p:ajax update="cap, regionContainer" listener="#{normalizationBean.setSelectedSuggestion}" event="itemSelect">
                <ui:param name="selectedSuggestion" value="${result}" />
            </p:ajax>
            <p:column>
                ${result.indirizzo}
            </p:column>
            <p:column>
                ${result.localita}
            </p:column>
        </p:autoComplete>

Converter:

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if(value==null || value.equals(""))
        return null;
    try{
        return addressSuggestions.getList().get(Integer.valueOf(value));
    } catch(Exception e){
        return null;
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if(value==null || value.equals(""))
        return null;

    return String.valueOf(addressSuggestions.getList().indexOf(value));
}

Bean:

    @Model
        public class NormalizationBean implements Serializable, Converter{
        private AddressSuggestionsListType addressSuggestions;
        private String address;

        public List<AddressSuggestionsType> loadSuggestions(String query) {

        // call the service which returns a list of suggestions
                    AddressSuggestionsListType result = ShellService.instance()
                            .getSuggestions(query);

                    if (result == null){
                        addressSuggestions.setErr("Could not connect to service");
                    } else {
                        addressSuggestions= result;
                    }

                }

                return addressSuggestions.getList();
            }

        public void setSelectedSuggestion(SelectEvent selected){
                AddressSuggestionsType addType = (AddressSuggestionsType) selected.getObject();
                this.address = addType.getAddress();
            }

// ...
}

Thanks in advance! :)

It was enough to change the attribute immediate from true to false. Apparently it was getting the object from itemValue and returning it's toString() representation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM