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

194
Visualizações
Load datatables with async fetch

i'm trying to use datatables plugin with data from api, i get the data with fetch javascript, do i need async await to store the data into datatables ? the result is no error just it won't show the data, i already put the datatables inialization inside the async function how do i do it properly ?

html code :

<table id='myTable' class="table table-inverse table-danger">
    <thead>
    <tr>
        <td>Index</td>
        <td>Value</td>
    </tr>
    </thead>
    <tbody class"demo">
    </tbody>
</table>

fetch code :

  var url = 'http://192.168.150.155:9900/meikel/_search/';

async function getapi(url) {

      const response = await fetch('http://192.168.150.155:9900/meikel/_search', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + btoa('elastic:7d76bd7b7ca96905e0195b2f3ecd1449b9b98f58be7c71ed7549739990fad6a8')
    },
    body: JSON.stringify({
        size: 10000,
        "query": {
            "match_all": {}
        }
        })
    })

      var data = await response.json();
      
      if (response) {

      }

      var i = 0;

      for(let i = 0; i < data.hits.hits.length; i++) {

      }

      let name = "";
      let company = "";

      for (let i = 0; i < data.hits.hits.length; i++) {
        name += "<tr '> <td><img style='width: 50px;' src='"+ data.hits.hits[i]._source.imagepath + "'></td>"+ "<td><a href='employees/?id="+ data.hits.hits[i]._id +"' class='text-dark text-decoration-none'><p href='employees/'>" + data.hits.hits[i]._source.name + "</p></a></td>"  + "<td>" + data.hits.hits[i]._source.nip + "</td>" + "<td>" + data.hits.hits[i]._source.companycode + "</td>" + "<td>" + data.hits.hits[i]._source.organization.division + "</td>" + "<td>" + data.hits.hits[i]._source.employee.status + "</td>" + " </tr>";

      }

      document.getElementsByClassName("demo")[0].innerHTML = name;

      $(document).ready( function () {
          $('#myTable').DataTable();
      } );

      }

getapi(url);
about 4 years ago · Juan Pablo Isaza
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