Home  >  Q&A  >  body text

Split the returned data by date

<p>I return my form as follows, where I have two expected results for the 12th and 13th. </p> <p><br /></p> <pre class="snippet-code-js lang-js prettyprint-override"><code>var data = [{ Designacao: "Micro-ondas1", Capitulo: "Cozinha", Data: "2023-08-12", }, { Designacao: "Exaustor cinzento 1", Capitulo: "", Data: "2023-08-12", }, { Designacao: "Mesa - Castanha -8 Lugares", Capitulo: "Sala", Data: "2023-08-13" }, { Designacao: "cama", Capitulo: "Quarto", Data: "2023-08-13", }, ]; var linha = ``; Object.keys(data).forEach(i => { Designacao = data[i].Designacao; Capitulo = data[i].Capitulo; Data = data[i].Data; if (Data != Data) { linha = `<div class="card-header">Data Passagem de Turno - ${Data}</div>`; } linha = `<div class="row col-md-12"> <div class="col-md-4"> <p class="form-label">Data </p> <input type="text" class="form-control" name="dataen" value="${Data}"> </div> <div class="col-md-3"> <p class="form-label">Código Utente </p> <input type="text" class="form-control" name="codigoen" value="${Capitulo}" disabled="disabled"> </div> <div class="col-12"> <p class="form-label">Diário de Enfermagem </p> <textarea rows="6" class="form-control" name="didiarenf" > ${Designacao} </textarea> </div>`; $(".histpturno").html(linha); })</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/ jquery.min.js"></script> <div class="histpturno"></div></code></pre> <p><br /></p> <p>I'm going to split the results by day. For example, before the 12th result, show the title inside the if, then only show the title again when the 13th result is returned </p> <p>I tried using if to achieve the desired effect but it doesn't work, it never returns the title. The title I am referring to is this inside if: </p> <pre class="brush:php;toolbar:false;"><div class="card-header">Data Passagem de Turno - ${Data}</div></pre> <p><br /></p>
P粉986028039P粉986028039433 days ago496

reply all(1)I'll reply

  • P粉512363233

    P粉5123632332023-08-15 10:10:49

    It should be obvious that this will never be true, right? Data does not change between read accesses on either side of the comparison operator.

    You need to compare the current value with the value of the previously processed record. The simplest way is to store the previous value into a variable and initialize it to a value that won't appear in the actual data (so it will return true when checked on the first record).

    var data = [{
        Designacao: "Micro-ondas1",
        Capitulo: "Cozinha",
        Data: "2023-08-12",
      },
      {
        Designacao: "Exaustor cinzento 1",
        Capitulo: "",
        Data: "2023-08-12",
      },
      {
        Designacao: "Mesa - Castanha -8 Lugares",
        Capitulo: "Sala",
        Data: "2023-08-13"
      },
      {
        Designacao: "cama",
        Capitulo: "Quarto",
        Data: "2023-08-13",
      },
    ];
    
    var linha = ``;
    var prevData = null; // initialize
    
    Object.keys(data).forEach(i => {
    
      Designacao = data[i].Designacao;
      Capitulo = data[i].Capitulo;
      Data = data[i].Data;
    
      if (Data !== prevData) { // compare; for safety using strict type checking
        linha += `<div class="card-header">Data Passagem de Turno - ${Data}</div>`;
      }
      prevData = Data; // update, so that this will hold the correct "previous" record on the next iteration
    
      linha += `<div class="row col-md-12">
                <div class="col-md-4">
                  <p class="form-label">Data </p>
                  <input type="text" class="form-control" name="dataen" value="${Data}">
                </div>
                <div class="col-md-3">
                  <p class="form-label">Código Utente </p>
                  <input type="text" class="form-control" name="codigoen" value="${Capitulo}" disabled="disabled">
                </div>
                <div class="col-12">
                  <p class="form-label">Diário de Enfermagem </p>
                  <textarea rows="6" class="form-control" name="didiarenf" > ${Designacao} </textarea>
                </div>`;
    
      $(".histpturno").html(linha);
    
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div class="histpturno"></div>

    reply
    0
  • Cancelreply