Search code examples
primefacesmanaged-bean

Autocomplete with Primefaces


i have the following Bean:

public List<Kunden> autocomplete(String nummer) {

    List<Kunden> allNummern = kundenDao.alleNummern();
    List<Kunden> gefiltert = new ArrayList<Kunden>();
    for (Kunden k : allNummern) {
        String convert = String.valueOf(k.getKundennummer());
        if (convert.startsWith(nummer)) {
            gefiltert.add(k);
        }
        else
        {   

            kunde.setKundennummer(Long.parseLong(nummer));
            kunde.setFirma("Noch nicht vergeben");
            gefiltert.add(kunde);
        }

    }

    HashSet<Kunden> hashSet = new HashSet<Kunden>(allNummern);
    gefiltert.clear();
    gefiltert.addAll(hashSet);


    return gefiltert;

}

And my DAO class looks like this:

public class KundenDAO implements Serializable{


private static final long serialVersionUID = 1L;

private EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("auditcheck 3.0");

public List<Kunden> alleNummern() {
    EntityManager em = emf.createEntityManager();
    Query q = em.createQuery("SELECT DISTINCT k FROM Kunden k");
    List<Kunden> nummern = q.getResultList();
    return nummern;

  }
}

And this is my View code:

<prime:autoComplete id="auto" value="#{neu.kundennummer}" autocomplete="false" minQueryLength="1" completeMethod="#{neu.autocomplete}" required="true" var="k" itemValue="#{k.kundennummer}" itemLabel="#{k.kundennummer}" forceSelection="true">
                    <prime:column>
                        <h:outputText value="#{k.kundennummer} - #{k.firma}" />
                    </prime:column>
                </prime:autoComplete> 

My problem is, that i want, that if the autocomplete Method didn't find some entry in the list, the value from the input field from the view, is used. But it dosen't work. All what happens is that can see all the entrys from my list and whren i start tipping some other number into and want to confirm, the value isn't show. I use a HashMap Collection (Number - Factory), which is shown to the user.


Solution

  • So why do you have forceSelection="true"? This attribute forces user to choose from existing options, if you remove it then user can type whatever he wants to.