Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Wait datatable to finish

jose

New Coder
hello, in javascript i need wait to table load all data content, my approach is whit "initComplete" but table is not loading.
here is my example code:

HTML:
<script>

    var myArray = [~alldata~]

    initComplete: buildTable(myArray)



    function buildTable(data){
        var table = document.getElementById('table')

        for (var i = 0; i < data.length; i++){
            var row = `<tr>
                            <td>type</td>
                            <td>${data[i].id}</td>
                            <td>${data[i].de}</td>                                            
                      </tr>`
            table.innerHTML += row


        }
    }

</script>

anyone have a sugestion?
 
Last edited by a moderator:
Hey there,
You could use the Array.prototype.map() method to map the data array to a new array with the table rows, before placing it in the table:
JavaScript:
function buildTable(data) {
  const table = document.getElementById("table");
  const tableRows = data.map((dataItem) => {
    `<tr>
       <td>type</td>
       <td>${dataItem.id}</td>
       <td>${dataItem.de}</td>
     </tr>`
  });
  table.innerHTML = tableRows.join();
}


JavaScript:
initComplete: buildTable(myArray)
Where did you get this from? Is this actually a thing in JavaScript? I've never seen anything like this being used in JavaScript, other than in an object, but this clearly is not an object.
 
initComplete seems to be a jQuery thingy. I am not familiar with jQuery, but have you made sure your buildTable() function is actually executed ? Put a console.log() statement in there, or step through it in the debugger, to be 100% sure.

Personally I would not append to table.innerHTML for every row. Append to a string, then after the loop is completed, assign that string to table.innerHTML

What exactly did you mean with "table is not loading" ?
 

New Threads

Buy us a coffee!

Back
Top Bottom