Autocomplete with New Registration option - JAVA

Asked

Viewed 179 times

0

I have a autoComplete on the sales screen, when the user enters the customer’s name I want that if it has no record it displays something like "ADD NEW CUSTOMER", I’m wearing primefaces:

 <p:autoComplete id="cliente"  required="true"
                                                    styleClass="Container100 Fnone MarAuto Fs15" placeholder="Cliente"
                                                    rendered="#{loginControler.vendaFutura eq true}"
                                                    completeMethod="#{pessoaControle.autoComplete}"
                                                    var="cli" onclick="this.select();" 
                                                    itemLabel="#{cli.nome}"
                                                    itemValue="#{cli.id.toString()}"
                                                    converter="#{pessoaControle.converter()}"
                                                    value="#{vendaControle.venda.pessoa}"
                                                    queryDelay="500"
                                                    dropdown="true" scrollHeight="550"
                                                    style="margin-right: 30px;">

                                        <p:ajax event="itemSelect" update="painelCpf totalReceber console totalReceberVencido painelpto" listener="#{vendaControle.enviaCpfCnpj()}"/>


                                        <f:facet name="itemtip">
                                            <h:panelGrid columns="2" cellpadding="5">

                                                <h:outputText value="CPF/CNPJ" />
                                                <h:outputText style="color: blue" value="#{cli.documentoFederal}" />

                                                <h:outputText value="RG/IE" />
                                                <h:outputText style="color: blue" value="#{cli.documentoEstadual}" />

                                                <h:outputText value="Tel" />
                                                <h:outputText style="color: blue" value="#{cli.fone}" />



                                            </h:panelGrid>
                                        </f:facet>


                                    </p:autoComplete>

My List

 public List<Pessoa> autoCompletePessoa(String campo, String cons) {
    cons = cons.toUpperCase();
    String consulta = "FROM Pessoa AS i"
            + " WHERE i.validaFuncionario is false AND i.ativo is true AND (i." + campo + ") LIKE ('" + cons + "%')"
            + " ORDER BY i." + campo;

    Query query = getEntityManager().createQuery(consulta);
    return query.setMaxResults(20).getResultList();
}

1 answer

0


Autocomplete does not have a Facet for emptyMessage, such as Datatable (which could make the job easier). To do what you want, you can try to create a Person with the id null and return in search. When the user selects, an add button will be rendered and will allow to complement the Person data.

In XHTML:

<p:autoComplete id="cliente"  required="true"
    styleClass="Container100 Fnone MarAuto Fs15" placeholder="Cliente"
    rendered="#{loginControler.vendaFutura eq true}"
    completeMethod="#{pessoaControle.autoComplete}"
    var="cli" onclick="this.select();" 
    itemLabel="#{cli.nome}"
    itemValue="#{cli.id.toString()}"
    converter="#{pessoaControle.converter()}"
    value="#{vendaControle.venda.pessoa}"
    queryDelay="500"
    dropdown="true" scrollHeight="550"
    style="margin-right: 30px;">

    <p:ajax event="itemSelect" update="painelCpf totalReceber console totalReceberVencido painelpto painelAdicionarPessoa" listener="#{vendaControle.enviaCpfCnpj()}"/>

    <f:facet name="itemtip">
        <h:panelGrid columns="2" cellpadding="5">

            <h:outputText value="CPF/CNPJ" />
            <h:outputText style="color: blue" value="#{cli.documentoFederal}" />

            <h:outputText value="RG/IE" />
            <h:outputText style="color: blue" value="#{cli.documentoEstadual}" />

            <h:outputText value="Tel" />
            <h:outputText style="color: blue" value="#{cli.fone}" />

        </h:panelGrid>
    </f:facet>

</p:autoComplete>

<h:panelGroup id="painelAdicionarPessoa">

    <p:commandButton value="#{label.adicionar}" actionListener="#{pessoaControle.acaoAdicionarPessoa()}" ajax="true" partialSubmit="true" rendered="#{vendaControle.venda.pessoa.id eq null}" />

</h:panelGroup>

In the controller:

public List<Pessoa> autoCompletePessoa(String campo, String cons) {
    cons = cons.toUpperCase();
    String consulta = "FROM Pessoa AS i"
            + " WHERE i.validaFuncionario is false AND i.ativo is true AND (i." + campo + ") LIKE ('" + cons + "%')"
            + " ORDER BY i." + campo;

    Query query = getEntityManager().createQuery(consulta);
    List<Pessoa> retorno = query.setMaxResults(20).getResultList();

    /* Cria um novo objeto se a listagem retornar vazia */
    if (retorno.isEmpty()) {
        Pessoa novaPessoa = new Pessoa();
        novaPessoa.setId(null);
        novaPessoa.setNome(cons)
        retorno.add(novaPessoa);
    }

    return retorno;
}

public void acaoAdicionarPessoa() {
    /* Código para adicionar a pessoa de vendaControle.venda.pessoa */
}

Note: It may be necessary to change your converter, because the Person id will be null.

  • Thanks for the answer, already solved my problem

Browser other questions tagged

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