no data available in table, data table disappears from rows when searching

no data available in table, data table disappears from rows when searching

rojaslozarojasloza Posts: 2Questions: 1Answers: 0

I'm reading a lot about this, the solution is always the same first fill the table and then call Datatable, but I can't get it to work for me, my problem is that it fills the table but Datatable doesn't recognize it, when writing to the box search the rows disappear and do not load again unless you refresh the browser

I tried to fill the table in different ways, I always get the same result.

How should I call Datatable to recognize the rows?

This is part of my code.

const DOMitems = document.querySelector('#items');
const productsUrl = 'http://localhost:5000/listproducts'

let fetchProducts = async () => {
   const response = await fetch(productsUrl);
   if (response.status !== 200) {
     throw new Error("cannot fetch data");
   let datashops = response.json();

   return datashops;

function  renderizarProductos() {
   .then((datashop)=> {
       datashop.forEach((item) => {  
           // Estructura
           const miNodo = document.createElement('tr');
           const miNodoCardBody = document.createElement('td');
           // Titulo
           const miNodoTitle = document.createElement('td');
         miNodoTitle.innerHTML += item.code_name; 
           // Imagen
           const miNodoimg = document.createElement('td');
           const miNodoImagen = document.createElement('img');
           miNodoImagen.setAttribute('src', '/images/products/'+item.image);
           // Precio
           const miNodoPrecio = document.createElement('td');
          /*  miNodoPrecio.classList.add('card-text'); */
           miNodoPrecio.innerHTML += (parseFloat(item.price.$numberDecimal)).toFixed(2);

           const miNodoStock = document.createElement('td');
           miNodoStock.innerHTML += item.stock;
           // Boton 
           const miNodoBtn = document.createElement('td');
           const miNodoBoton = document.createElement('button');
           miNodoBoton.classList.add('btn', 'btn-primary');
           miNodoBoton.innerHTML += '+';
           miNodoBoton.setAttribute('marcador', item._id);
           miNodoBoton.addEventListener('click', addProductsToCar); 
           // Insertamos
       /*     miNodoimg.appendChild(miNodoImagen); */
    /*        miNodo.appendChild(miNodoimg);   */                      


html showing datable

<table id="sellproducts" class="table" style="width:100%">
                            <tr class="text-dark">
                        <tbody id="items">

                            <tr class="text-dark">

This question has an accepted answers - jump to answer


  • kthorngrenkthorngren Posts: 19,125Questions: 26Answers: 4,534
    edited July 2022 Answer ✓

    The problem is ajax is an asynchronous process so with your code the Datatables init in line 63 actually happens before the ajax process is complete. Meaning before the then function in line 16 executes. Move the Datatables init statement inside this function, something like this:

       .then((datashop)=> {
           datashop.forEach((item) => { 

    This way it will execute after the loop populating the table is complete.


  • rojaslozarojasloza Posts: 2Questions: 1Answers: 0

    Thank you very much, I was stuck for several days on this

Sign In or Register to comment.