动态创建表列标题 [英] Create table column-headers dynamically

查看:68
本文介绍了动态创建表列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我无法弄清楚如何为该代码中动态创建html表的所有列添加标题

Right now I cannot figure out how to add headers for all the columns in this code where a html-table are created dynamically

function createTableAndInsert(sqlArray) {

//create table dynamically - only one row by the moment
var table = document.createElement('table');
for (var i = 1; i < 2; i++) {

    var tr = document.createElement('tr'); // row
    for (var j = 0; j < 8; j++) {

        var td = document.createElement('td'); //column
        var text = document.createTextNode(sqlArray[j]); //cell
        td.appendChild(text);
        tr.appendChild(td);
    }

    table.appendChild(tr);
}
document.getElementById('single_fine_view').appendChild(table);

}

推荐答案

您可以将标头列表分别传递给函数并添加以下代码

You can pass the header list separately to your function and add following code

function createTableAndInsert(sqlArray,headerList) {
var table = document.createElement('table');
var tr = document.createElement('tr'); // Header row
for (var j = 0; j < 8; j++) {

    var th = document.createElement('th'); //column
    var text = document.createTextNode(headerList[j]); //cell
    th.appendChild(text);
    tr.appendChild(th);
}

table.appendChild(tr);
for (var i = 1; i < 2; i++) {

var tr = document.createElement('tr'); // row
for (var j = 0; j < 8; j++) {

    var td = document.createElement('td'); //column
    var text = document.createTextNode(sqlArray[j]); //cell
    td.appendChild(text);
    tr.appendChild(td);
}

table.appendChild(tr);
}
document.getElementById('single_fine_view').appendChild(table);
}

这篇关于动态创建表列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆