Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Convert Josn data to html table

I'm using jsreport and i want to convert json data to html table but i couldnt manage to do it. This my data :

    {
"items": [
    {
        "order": "65854",
        "addr": "test 1",
        "city": "2fc2",
        "country": "2fc2"
    },
    {
        "order": "75757",
        "addr": "azerty",
        "city": "2fc2",
        "country": "2fc2"
    },
    {
        "order": "65575784",
        "addr": "tst",
        "city": "2fc2",
        "country": "2fc2"
    }
]
}

And this is my code :

<h1>Test</h1>
       
<table class='table striped'>
    <thead>
        <tr>
            <th>OrderID</th>
            <th>ShipAddress</th>
            <th>ShipCity</th>
            <th>ShipCountry</th>
        </tr>
    </thead>
    <tbody>
        {{#each row}}
        <tr>
            <td>{{order}}</td>
            <td>{{addr}}</td>
            <td>{{city}}</td>
            <td>{{country}}</td>
        </tr>
        {{/each}}
    </tbody>
</table>

<script>

for (let i=0; i<items.length; i++) {
   let row = items[i]
}
</script>

i think the problem is in {{#each row}} {{/each}} and its not getting any thing.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It makes a lot of difference what the error message says. (Like, if it tells you a certain variable doesn't exist, you know you need to declare that variable in the scope where the error message was provided.) You'll get used to making use of error messages to debug your code.

For now, here's a working demo (using vanilla JS) that does something very similar to what you're trying to do. Two issues resolved here are:

  • You may not be getting the items property (an Array) from your JSON object before you try to loop through it (maybe shown in your first error message)
  • You use var to declare a for-loop counter, a classic trap

const
  inputObject = { "items": [ {id: 1}, {id: 2}, {id: 3} ] },
  items = inputObject.items, // Gets the `items` array
  TRs = document.querySelector("tbody").children; // (For demo)

for (let i = 0; i < items.length; i++) { // Uses `let`, not `var`
  const
    row = items[i],
    TD = TRs[i].children[0]; // Gets first td in table row
  TD.textContent = row.id;
}
<table class='table striped'>
    <thead>
        <tr> <th>OrderID</th> </tr>
    </thead>
    <tbody>
        <tr><td></td></tr>
        <tr><td></td></tr>
        <tr><td></td></tr>
    </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda