Surfaces, auto-complete, multiple modes. How to avoid selecting the same item twice?

I know that I cannot do this directly in Primefaces, I understand that I need to do this in the converter, but I don’t know at what stage and how? And what exactly should I check? Maybe for this I need to wedge into the JSF life cycle? For example, after p: Autocomplete, add the item to the list in the " Apply phase request values ". I have to check if there is a duplicate element and delete it before β€œ Update model parameter values ” if I understand the JSF life cycle correctly? Is it even possible? Thank you in advance.

+6
source share
1 answer

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 { /** * The items currently available for selection */ private List<String> items = new ArrayList<String>(); /** * Current selected items */ private List<String> selectedItems = new ArrayList<String>(); /** * All the items available in the application */ private List<String> allItems = new ArrayList<String>(); /** * Create a hardcoded set of items and add all of them for selection */ public AutoCompleteBean() { allItems.add("item1"); allItems.add("item2"); allItems.add("item3"); allItems.add("item4"); items.addAll(allItems); } /** * Check the current user query for selection. If it fits any of the items * of the system and it not already selected, add it to the filtered List * * @param query * @return */ 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; } } 
+10
source

Source: https://habr.com/ru/post/973128/


All Articles