How to insert table rows and columns with 2d array

后端 未结 2 1282
情深已故
情深已故 2021-01-28 02:08

SO I have 2 arrays which are var num = [\"1\", \"2\", \"3\"]; var cars = [\"Saab\", \"Volvo\", \"BMW\"];
and I made a button where it\'ll add rows and column and pl

相关标签:
2条回答
  • 2021-01-28 02:29

    This is a simple solution using your approach.

    function myFunction() {
    
      var num = ["1", "2", "3"];
      var cars = ["Saab", "Volvo", "BMW"];
      var table = document.getElementById("myTable");
      var rows = new Array(cars.length);
      var numCell, carCell;
    
      for (var i = 0; i < cars.length; i++) {
        rows[i] = table.insertRow(i);
        numCell = rows[i].insertCell(0);
        carCell = rows[i].insertCell(1);
        numCell.appendChild(document.createTextNode(num[i]));
        carCell.appendChild(document.createTextNode(cars[i]));
      }
    
    }
    <button onclick="myFunction()">Try it</button>
    
    <table id='myTable'></table>

    0 讨论(0)
  • 2021-01-28 02:40

    For a while now i use the below generic code to generate table HTML. It will take an array of objects and object properties will become table header and values will become cells. So if your data was in the form

    var myCars = [{Pos: 1, Make: "Saab"}, {Pos: 2, Make: "Volvo"}, {Pos: 2, Make: "BMW"}] then we would get a table like

    var tableMaker = (o,h) => {var keys = Object.keys(o[0]),
                               rowMaker = (a,t) => a.reduce((p,c,i,a) => p + (i === a.length-1 ? "<" + t + ">" + c + "</" + t + "></tr>"
                                                                                               : "<" + t + ">" + c + "</" + t + ">"),"<tr>"),
                               rows = o.reduce((r,c) => r + rowMaker(keys.reduce((v,k) => v.concat(c[k]),[]),"td"),h ? rowMaker(keys,"th") : []);
                               return "<table>" + rows + "</table>";
                               };
            myCars = [{Pos: 1, Make: "Saab"}, {Pos: 2, Make: "Volvo"}, {Pos: 2, Make: "BMW"}] ,
             table = tableMaker(myCars,true); // if second argument provided as truthy then headers generated
    document.write(table);

    However we don't have a similar data at hand. So lets make it. We don't need a header so just pass tableMaker function it's second parameter as false. Then the only thing left to us is to generate the data in the form of a 2D array as the first argument. Let's do it;

    var tableMaker = (o,h) => {var keys = Object.keys(o[0]),
                               rowMaker = (a,t) => a.reduce((p,c,i,a) => p + (i === a.length-1 ? "<" + t + ">" + c + "</" + t + "></tr>"
                                                                                               : "<" + t + ">" + c + "</" + t + ">"),"<tr>"),
                               rows = o.reduce((r,c) => r + rowMaker(keys.reduce((v,k) => v.concat(c[k]),[]),"td"),h ? rowMaker(keys,"th") : []);
                               return "<table>" + rows + "</table>";
                               },
               num = ["1", "2", "3"],
              cars = ["Saab", "Volvo", "BMW"],
             tdata = num.map((e,i) => [e,cars[i]]); // this is your 2D array.
             table = tableMaker(tdata,false), // if second argument provided as truthy then headers are generated
                
    document.write(table);

    0 讨论(0)
提交回复
热议问题