Perhaps what you need to do to update the model for each user selection / deselection. This is done using the <p:ajax /> in <p:autoComplete /> , so the List selected items will be updated in the background. Later, when the user requests other requests, think of this List .
Check SSCCE for List of String (you can use Converter or not for your custom classes, but this is not at all related to your question):
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head /> <h:body> <h:form> <p:outputLabel value="Multiple:" /> <p:autoComplete multiple="true" value="#{autoCompleteBean.selectedItems}" completeMethod="#{autoCompleteBean.completeItem}" var="it" itemLabel="#{it}" itemValue="#{it}" forceSelection="true"> <p:ajax event="itemSelect" /> <p:ajax event="itemUnselect" /> <p:column> <h:outputText value="#{it}" /> </p:column> </p:autoComplete> </h:form> </h:body> </html>
@ManagedBean @ViewScoped public class AutoCompleteBean { private List<String> items = new ArrayList<String>(); private List<String> selectedItems = new ArrayList<String>(); private List<String> allItems = new ArrayList<String>(); public AutoCompleteBean() { allItems.add("item1"); allItems.add("item2"); allItems.add("item3"); allItems.add("item4"); items.addAll(allItems); } public List<String> completeItem(String query) { List<String> filteredList = new ArrayList<String>(); for (String item : allItems) { if (item.startsWith(query) && !selectedItems.contains(item)) { filteredList.add(item); } } return filteredList; } public List<String> getItems() { return items; } public List<String> getSelectedItems() { return selectedItems; } public void setSelectedItems(List<String> selectedItems) { this.selectedItems = selectedItems; } }
source share