Create td depending "X" number that I pass

0

I want to create in a table X number of <td> depending on an X number that I pass, for which I have the following function.

function holaMateriales(){
  var cont =13;
  peticionDatos(
    '<?= base_url(); ?>index.php/modulos/supervisor/supinicio/datos',
      {},
    function(){},
    function(datos){

         var matT = "<tr></td>"; 

         $(datos).each(function(i,e){

            matT += "<td id='c-"+e.mcID+"'>"+ e.mcNombre+"</td>"+
            "<td id='c-"+e.presID+"'>"+ e.pres+"</td>"+td+"</tr>";
           console.log(cont);
          });
          $("#materiales").append(matT);

        });
}

what it does is create X number of <tr> depending on what happens to it, now what I want is to create 15 <td> for each <tr>

  

| Mat | Des | td1 | td2 | etc | etc .....

until the 15 that I want are created, which will start after Des

Someone who can help me ...?

    
asked by Soldier 15.08.2017 в 00:55
source

1 answer

0

There are several issues here:

In my opinion this line var matT = "<tr></td>"; should not carry the </td> and should be within the cycle each . The <tr> must be in a cycle for the rows to be created, within that cycle and the 15 td of opening and closing with its content must be put.

You can not make indented tables (well, they should not be done), because you will have many problems. If you define 15 columns, all rows will have 15 columns, or if they are 10, they will all have ten. If you know the number of columns put the 15, but if you can vary in each construction of the table, you need another cycle.

But in order for you to receive a good help we need to see that it brings the variable datos .

I create an empty table in javascript that is then filled with a jquery datatables as follows:

var txtTemp;
var thTemp;
var tblHead = document.createElement("thead");
var filaHead = document.createElement("tr");
var encabezados=new Array("COLUMNA1","COLUMNA2","COLUMNA3","COLUMNAN",);
tabla = document.createElement("table");
tabla.setAttribute("cellpadding", "0");
tabla.setAttribute("cellspacing", "0");
tabla.setAttribute("border", "0");
tabla.setAttribute("id", "tblRevalidacion");
tabla.setAttribute("class", "display");
tabla.setAttribute("width", "100%");
filaHead.setAttribute("class", "display");
for(var i=0;i<encabezados.length;i++){
        txtTemp=document.createTextNode(encabezados[i]);
        thTemp=document.createElement("th");
        thTemp.appendChild(txtTemp);
        filaHead.appendChild(thTemp);
}   
tblHead.appendChild(filaHead);
tabla.appendChild(tblHead);

var divAplicacion = document.getElementById("aplicacion");
divAplicacion.appendChild(tabla);

To do this add a div id=aplicación to the html and the table is generated within it. My table only has the headers because I'll tell you later I'll fill it with a jquery datatables. It would also be good if you made a console.log(matT) to see how your table is and do not walk blind.

    
answered by 15.08.2017 в 01:37