简体   繁体   中英

Using ListConverter with forceSelection=false

I am using <p:autocomplete> tags and omnifaces converters in order to let the user choose addresses from a list of given addresses in the system. Below is the sample .xhtml code for two address fields:

   <p:autoComplete id="name" completeMethod="#{viewBean.autocompleteName}" var="_address"
        value="#{viewBean.draft.address}" itemValue="#{_address}"
        itemLabel="#{_address.name}">
        <o:converter converterId="omnifaces.ListConverter" list="#{viewBean.addresses}" />
         <p:column>
            <h:outputText value="#{_address.name}" />
        </p:column>
        <p:column>
            <h:outputText value="#{_address.street}" />
        </p:column>
        <p:ajax event="itemSelect" listener="#{viewBean.onAddressSelected}" update="addressPanel" />
    </p:autoComplete>

    <p:autoComplete id="street" completeMethod="#{viewBean.autocompleteStreet}" var="_address"
        value="#{viewBean.draft.address}" itemValue="#{_address}"
        itemLabel="#{_address.street}">
        <o:converter converterId="omnifaces.ListConverter" list="#{viewBean.addresses}" />
        <p:column>
            <h:outputText value="#{_address.name}" />
        </p:column>
        <p:column>
            <h:outputText value="#{_address.street}" />
        </p:column>
        <p:ajax event="itemSelect" listener="#{viewBean.onAddressSelected}" update="addressPanel" />
    </p:autoComplete>

Upon selection, the other fields (for the other address attributes) are filled automatically. So far, that works fine.

Here comes the problem: I would like to allow input that is different from any exising item in the list of options ( forceSelection="false" ). Let's say the user choses an existing address but then changes the name of the street and saves it.

Currently, using the ListConverter, this change is simply ignored and the object stays the same.

The behaviour I want is that the business object ( viewBean.draft.address ) is modified and saved. In other words, the new object is not anymore a member of the list of options that I provided to the converter.

Is there a way to realize this behaviour but still using the benefits of the omnifaces.ListConverter ? Or do I have to write my own converter completely from scratch?

Easiest is to just add the query to the list.

public List<Address> autocompleteStreet(String query) {
   addresses = addressService.listByStreet(query);
   addresses.add(0, new Address(query)); // <-- Just add query to list.
   return addresses;
}

Update based on comments, PF appears to be buggy when dealing with forceSelection="false" and converted item values. Your best bet is to extend OmniFaces ListConverter as below and then use it instead.

@FacesConverter("addressListConverter")
public class AddressListConverter extends ListConverter {

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

        return ((Address) value).getStreet();
    }

}

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