如何将数据插入jquery数据表?

时间:2012-04-04 11:32:14

标签: jquery datatable jquery-datatables

我曾尝试在datatable中插入数据,但是javascript控制台说这个表是未定义的。这是我的代码:

// ----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 个答案:

答案 0 :(得分:1)

DOM中是否存在id为'tabla1'的表?如果没有,您可以动态创建它:

原生JavaScript:

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

jQuery的:

var tabla1 = $('<table></table>', { id: 'tabla1' });

答案 1 :(得分:0)

如果是这种情况,您是否要创建jquery Datatable,那么您必须包含

页面中的jquery.dataTables.min.js或jquery.dataTables.js