Primefaces AutoCompleteBean丢失了所选对象的列表

时间:2014-03-05 08:43:08

标签: jsf-2 primefaces autocomplete

我必须使用多个选项进行自动完成,所有选定的项目必须是唯一的。我想在completeRole函数中过滤已选择的值。但是completeRole函数获取null selectedRoles列表。我做错了什么?每次我想在自动完成输入中选择项目时,AutoCompleteBean是否都是新建的,带有空列表?我该如何解决这个问题?

AutoCompleteBean.java

package primefaces.beans;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import bpm.beans.entities.Roles;

@ManagedBean(name = "autoCompleteBean")
public class AutoCompleteBean {

    private List<Roles> rolesDB;
    private List<Roles> selectedRoles;

    public AutoCompleteBean() {
        rolesDB = RolesBean.getRolesDB();
    }

    public List<Roles> completeRole(String query) {
        List<Roles> suggestions = new ArrayList<Roles>();  

        List<Roles> rl = getSelectedRoles();
        for(Roles p : rolesDB) {  
            if(p.getTitle().startsWith(query)) 
                if(rl==null){
                    suggestions.add(p);  
                }
                else if (rl.indexOf(p) < 0)
                    suggestions.add(p); 
        }  
        return suggestions;  
    } 
}

roles.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
    <h:form id="form">
        <p:autoComplete value="#{autoCompleteBean.selectedRoles}" completeMethod="#{autoCompleteBean.completeRole}"  
            var="p" itemLabel="#{p.title}" itemValue="#{p}" converter="rolesConverter" multiple="true" 
            id="autoComplete" >  
            <p:column style="width:100%">#{p.title}</p:column>  
        </p:autoComplete> 
    </h:form>
</h:body>
</html>

1 个答案:

答案 0 :(得分:0)

要保留多个请求的选择,您的bean必须至少为@ViewScoped

您必须处理您的选择并更新您的bean实例。添加ajax:

<p:autoComplete value="#{autoCompleteBean.selectedRoles}" completeMethod="#{autoCompleteBean.completeRole}"  
        var="p" itemLabel="#{p.title}" itemValue="#{p}" converter="rolesConverter" multiple="true" 
        id="autoComplete" >  

    <p:ajax event="itemSelect" process="@this" update="@this"/>

    <p:column style="width:100%">#{p.title}</p:column>  
</p:autoComplete>