简体   繁体   English

将 ListConverter 与 forceSelection=false 一起使用

[英]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.我正在使用<p:autocomplete>标签和 omnifaces 转换器,以便让用户从系统中给定地址的列表中选择地址。 Below is the sample .xhtml code for two address fields:下面是两个地址字段的示例 .xhtml 代码:

   <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" ).问题来了:我想允许与选项列表中的任何现有项目不同的输入( 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.目前,使用 ListConverter,此更改将被简单地忽略并且对象保持不变。

The behaviour I want is that the business object ( viewBean.draft.address ) is modified and saved.我想要的行为是修改并保存业务对象( viewBean.draft.address )。 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 ?有没有办法实现这种行为,但仍然使用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.根据评论更新,PF 在处理forceSelection="false"和转换的项目值时似乎有问题。 Your best bet is to extend OmniFaces ListConverter as below and then use it instead.最好的办法是按如下方式扩展 OmniFaces ListConverter ,然后改用它。

@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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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