0

I have tried to insert data in datatable but javascript consoloe says that this table is undefined. Here is my code:

// ----creating the table----
        miArray = [[11,22,33,44,55,"22/11/2012",99,86,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111]]

            var tabla= "<table name='tabla1' id='dataTable'><thead><tr><th>Tipo de documento</th><th>Tipo de documento</th><th>Tipo de documento</th><th>Codigo de documento</th><th>Descripcion de documento</th><th>Fecha de documento</th><th>Codigo de proveedor</th><th>Descripcion de proveedor</th><th>Importe</th><th>Importe IVA</th><th>Total a pagar</th></tr></thead><tbody>"

            for (i=0;i<miArray.length;i++){ 
                    tabla = tabla + '<tr>'
                for (j=0;j<miArray[i].length;j++){              
                    tabla = tabla +'<td></td>'                      
                }   
                tabla = tabla + '</tr>'     
            }
            tabla = tabla +'</tbody></table>'

            document.getElementById("tabla1").innerHTML = tabla

            // -----inserting data--------
            h=1
            for (i=0;i<miArray.length;i++){
                for (j=0;j<miArray[i].length;j++){          
                    document.getElementById("tabla1").rows[h].cells[j] = miArray[i][j]          
                }   
            h++ 
            }

2 Answers 2

1

Does the table with the id of 'tabla1' exist in the DOM? If not, you can create it on the fly:

Native JavaScript:

var tabla1 = document.createElement('table');
tabla1.id = 'tabla1';

jQuery:

var tabla1 = $('<table></table>', { id: 'tabla1' });
Sign up to request clarification or add additional context in comments.

3 Comments

ok, there is a div in the dom width 'tabla1' id. IT´s not the best name for it...
It´s done. I put the id of the div in stead of the id of the table. Thanks for your answer
First of all, ensure the script is executed when the document has fully loaded, therefore using an onload trigger or placing the script at the bottom of the page would suffice. Secondly, using the properties .rows and .cells on the div will not work. The div is not a table, therefore these properties are void.
0

Do you want to create jquery Datatable if this is the case then you have to include

jquery.dataTables.min.js or jquery.dataTables.js in your page

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.