AutoComplete with New Registration option - JAVA

0

I have an autoComplete on the sale screen, when the user types the customer name I want it if it does not have any records it displays something like ADD NEW CUSTOMER, could anyone help me? I'm using 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();
}
    
asked by anonymous 23.07.2018 / 22:26

1 answer

0

Autocomplete does not have a facet for the emptyMessage, just like the 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 the search. When the user selects, a button to add will be rendered and will allow complementing the data of the Person.

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 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 convert, since the id of the Person will be null.

    
09.08.2018 / 17:34