Show suggestions while typing autocomplete

Asked

Viewed 252 times

0

I got the following autoComplete :

<p:autoComplete id="geracao" value="#{habilidademb.habilidade.geracao}"
    completeMethod="#{habilidademb.listarGeracoes()}"
    dropdown="true" var="bean" itemLabel="#{bean.nome}" 
    itemValue="#{bean}" converter="#{geracaoConverter}" effect="bounce"/>

When I’m typing in it, it opens all the options and marks the ones that match the text I’m typing. I would like it only to show the options that match my text. How can I do that? Mine completeMethod is:

public List<Geracao> listarGeracoes() throws Exception 
{    
    this.geracoes = gDao.findAll();
    return this.geracoes;
}
  • Take a look here: http://www.primefaces.org/showcase/input/autoComplete.xhtml

  • I’ve looked but I don’t understand how I’m gonna do it.

1 answer

1


You can try the following:

public List<Geracao> listarGeracoes(String query) throws Exception
{
    /*
     * 1a. Opção: fazer um método no seu dao para buscar por nome diretamente no banco
     */
    //return gDao.buscarPorNome(query);

    /*
     * 2a. Opção: filtrar os resultados com base na query
     */
    List<Geracao> retorno = new ArrayList<Geracao>();
    for (Geracao g : gDao.findAll()) {
        String teste = g.getNome();
        if (teste.toLowerCase().contains(query.toLowerCase())) {
            retorno.add(g);
        }
    }
    return retorno;
}

And in your autocomplete, modify the completeMethod="#{habilidademb.listarGeracoes}" and add minQueryLength="3" to search from 3 characters.

  • And what parameter I pass when calling the method ?

  • Sorry, you have to modify completeMethod to: "#{habilidademb.listarGeracoes}"

  • Show, it worked, because you have to take the () of the method ?

  • I’m glad it worked. I think the component should look by default for a method with a string parameter implicitly, similar to listeners with parameters, which also do not need the ().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.