Hi Mike. I tried many things, including beyond your tip, but I can't make 
any progress. I think it's something simple. Attached is my last attempt. 
If you can help I would appreciate it.

Em quinta-feira, 23 de novembro de 2023 às 18:26:37 UTC-3, Mike Orr 
escreveu:

> On Thu, Nov 23, 2023 at 11:50 AM Oberdan Santos <[email protected]> 
> wrote:
> >
> > You should note in the subject statement that in addition to the query, 
> I have the problem of the result being published on another page.
> > query page code
> > # templates/pac_recepx.jinja2
> >
> > <div class="form">
> > <div class="row g-2 mt-3">
> > <h3><span class="font-semi-bold">Consultar cadastro do 
> paciente</span></h3>
> > </div>
> > <form class="form-inline my-2 my-lg-0" action="
> http://localhost:6543/queryx"; method="GET">
> > <label for="cpf">Digite o CPF (11 números)</label>
> > <input class="form-control mr-sm-2" type="text" id="cpf" name="cpf" 
> required maxlength="11" value=''>
> > <button class="btn btn-outline-success my-2 my-sm-0" 
> type="submit">Consultar</button>
> > </form>
> > </div>
> >
> > The result is going to...
> > action="http://localhost:6543/queryx
> >
> > How do I take this result to the same page as the query 
> (templates/pac_recepx.jinja2), that is, place it below the query?
>
> There are two approaches.
>
> SERVER-SIDE ONLY:
>
> Remove the form `action` attribute. The form will post back to the
> same view that contained the form. In the view, add an `if` stanza to
> distinguish whether there's form input or not:
>
> ```
> cpf = request.params.get("cpf", "") # User input, or "" if no input.
> error = "" # Validation error
> message, or None if no error.
> rows = None # Result rows, or None
> if no valid input, or [] if valid input but zero results.
> if cpf: # If value is not "" or None.
> if CPF_IS_VALID:
> rows = request.dbsession...
> else:
> error = "Input is invalid."
> return {"cpf": cpf, "error": error, "rows": rows}
> ```
>
> Then your template might always show the form, but only show the
> results section if there was input, and only show the results table if
> there was at least one result, and only show the error message if
> there was a user error. I use Mako templating so I'll write it that
> way.
>
> ```
> ## page.mako
> <form method="GET" class="...">
> % if error:
> <div>${error}</div>
> %endif
> <input name="cpf" value="${cpf}" ... />
> </form>
>
> % if results is not None: # If there was valid user input.
> <h2>Results</h2>
> % if results: # If there was at least one result.
> <table>
> <th>Header</th>...
> % for r in results:
> <tr>...</tr>
> % endfor
> % else: # Else there were zero results.
> <p><em>No results.</em></p>
> % endif
> ```
>
> CLIENT-SIDE ALTERNATIVE:
>
> Write Javascript to intercept the Submit click. Send an AJAX request
> to the server to get the results in a JSON array. Use Javascript to
> populate the results table. That's beyond the scope of this mailing
> list. In this case you'd have a view that processes the AJAX request
> and converts the rows list to JSON before returning it.
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/05058659-a202-4b6b-b845-91514acc0169n%40googlegroups.com.
//templates/pac_recepx.jinja2   # arquivo principal de consulta

{% extends "basefull.jinja2" %}

{% block container %}
<hr />
<div class="container">
    <div class="text-center">
        <div class="btn-group" role="group" aria-label="Basic example">
            <button type="button" class="btn btn-primary">UF: Maranhão</button>
            <button type="button" class="btn btn-primary">REG_SAUDE: São 
Luis</button>
            <button type="button" class="btn btn-primary">MUN: São Luis</button>
            <button type="button" class="btn btn-primary">UBS: São 
Francisco</button>
            <div id="current_date">
            <script>
                date = new Date();
                year = date.getFullYear();
                month = date.getMonth() + 1;
                day = date.getDate();
                document.getElementById("current_date").innerHTML = day + "/" + 
month + "/" + year;
            </script>
            </div>
        </div>
    </div>

<hr />

    <div class="form">
        <div class="row g-2 mt-3">
            <h3><span class="font-semi-bold">Consultar cadastro do 
paciente</span></h3>
        </div>
        <form class="form-inline my-2 my-lg-0" 
action="http://localhost:6543/queryx"; method="GET" id="consultaForm">
            <label for="cpf">Digite o CPF (11 números)</label>
            <input class="form-control mr-sm-2" type="text" id="cpf" name="cpf" 
require maxlength="11" value=''>
            <button class="btn btn-outline-success my-2 my-sm-0" type="button" 
onclick="consultarCPF()">Consultar</button>
        </form>
    </div>

    <!-- Tabela de pacientes -->
    <div id="tabelaPacientes">
        <table border=1 id="pacientesTable">
            <tbody>
                {% for Paciente in pacientes %}
                    <tr>
                    <td>{{ Paciente.id }}</td> <td>{{ Paciente.name }}</td> 
<td>{{ Paciente.idade }}</td> <td>{{ Paciente.data_nascimento }}</td> <td>{{ 
Paciente.sexo }}</td>    
                    <td>{{ Paciente.raca }}</td> <td>{{ Paciente.fone }}</td> 
<td>{{ Paciente.endereco }}</td> <td>{{ Paciente.cpf }}</td> <td>{{ 
Paciente.cns }}</td>
                    <td><a href="/show/{{ Paciente.id }}">edit</a></td>
                    <td><a href="/delete/{{ Paciente.id }}">delete</a></td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <script>
        function consultarCPF() {
            var cpf = document.getElementById("cpf").value;
            if (cpf !== "") {
                // Realizar a requisição para buscar os pacientes com o CPF
                $.ajax({
                    type: "GET",
                    url: "http://localhost:6543/queryx";, // Sua URL de consulta
                    data: { cpf: cpf },
                    success: function (data) {
                        // Atualizar a tabela de pacientes com o resultado da 
consulta
                        $('#pacientesTable tbody').empty(); // Limpar tabela
                        if (data.pacientes.length > 0) {
                            data.pacientes.forEach(function (paciente) {
                                $('#pacientesTable tbody').append(
                                    '<tr>' +
                                    '<td>' + paciente.id + '</td>' +
                                    '<td>' + paciente.name + '</td>' +
                                    '<td>' + paciente.idade + '</td>' +
                                    '<td>' + paciente.data_nascimento + '</td>' 
+
                                    '<th>' + paciente.sexo + '</th>' +
                                    '<th>' + paciente.raca + '</th>' +
                                    '<th>' + paciente.fone + '</th>' +
                                    '<th>' + paciente.endereco + '</th>' +
                                    '<th>' + paciente.cpf + '</th>' +
                                    '<th>' + paciente.cns + '</th>' +
                                    '<th>' + paciente.Edit + '</th>' +
                                    '<th>' + paciente.Delete + '</th>' +
                                    '</tr>'+
                                );
                            });
                        } else {
                            $('#pacientesTable tbody').append('<tr><td 
colspan="12">Nenhum paciente encontrado.</td></tr>');
                        }
                    },
                    error: function () {
                        alert("Ocorreu um erro ao consultar o CPF.");
                    }
                });
            } else {
                alert("Por favor, insira um CPF válido.");
            }
        }
    </script>
</div>
{% endblock container %}
// template.pac_query.jinja2  # arquivo, usado para apresentar o resultado da 
tabela

<html>
<body>
<table border=1>
   <thead> 
      <tr>
         <th>id</th>
         <th>name</th>
         <th>idade</th>
         <th>data_nascimento</th>
         <th>sexo</th>
         <th>raca</th>
         <th>fone</th>
         <th>endereco</th>
         <th>cpf</th>
         <th>cns</th>
         <th>Edit</th>
         <th>Delete</th>
      </tr> 
   </thead>
   <tbody>
      {% for Paciente in pacientes %}
         <tr>
         <td>{{ Paciente.id }}</td> <td>{{ Paciente.name }}</td> <td>{{ 
Paciente.idade }}</td> <td>{{ Paciente.data_nascimento }}</td> <td>{{ 
Paciente.sexo }}</td>    
         <td>{{ Paciente.raca }}</td> <td>{{ Paciente.fone }}</td> <td>{{ 
Paciente.endereco }}</td> <td>{{ Paciente.cpf }}</td> <td>{{ Paciente.cns 
}}</td>
         <td><a href="/show/{{ Paciente.id }}">edit</a></td>
         <td><a href="/delete/{{ Paciente.id }}">delete</a></td>
         </tr>
      {% endfor %}
   </tbody>
</table>
</body>
</html>
// um dos decoradores da minha view


@view_config(route_name='queryp', renderer='piprdc:templates/pac_query.jinja2')
def queryp(request):
   cpf = request.params["cpf"]
   rows = request.dbsession.query(Paciente).filter(Paciente.cpf==cpf).all()
   pacientes=[]
   for row in rows:
      pacientes.append({"id":row.id, "name":row.name, "idade":row.idade, 
"data_nascimento":row.data_nascimento, "sexo":row.sexo, 
                        "raca":row.raca, "fone":row.fone, 
"endereco":row.endereco, "cpf":row.cpf, "cns":row.cns})
   return{'pacientes':pacientes}

Reply via email to