javascript 如何为动态表添加标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11062735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:59:44  来源:igfitidea点击:

How to add header for dynamic table

javascriptjqueryhtmljquery-ui

提问by PPD

Hello everybody I am drawing dynamic table and I want to add header for table how to add header to dynamic table here is my table code:

大家好,我正在绘制动态表,我想为表添加表头如何将表头添加到动态表这里是我的表代码:

$.get('http://myDomain.com/RIA/topIndiaDetails.asp', function(data)
{    
    console.log("####"+data);

    var up = new Image();
    var down = new Image();

    up.src = "../../jquery.mobile/images/up.png";
    down.src = "../../jquery.mobile/images/down.png"

    substr = data.split('$');

    //alert(substr.length);
    //var theader = '<table border="1">\n';
    //<table id="auditOverview" border="1">
    var theader = '<table border="1" id=\"tableId\">\n';
    var tbody = '';

    for (var out = 1;out<substr.length-1;out++)
    {
        //alert(substr[out]);
        tbody += '<tr>';
        var pra = substr[out].split('|^');
        //alert('pra.length is: '+pra.length);

        for (var i=0;i<pra.length-1;i++)
        {
            tbody += '<td>';

            if (pra[i]=="Red")
            {
                pra[i].append("<img id='theImg' src='../../jquery.mobile/images/down.png'/>");
            }
            else if (pra[i]=="Green")
            {
                pra[i].append("<img id='theImg' src='../../jquery.mobile/images/up.png'/>");
            }
            tbody += pra[i];    
            tbody += '</td>'  
        }
        tbody += '</tr>\n';
    }       
    var tfooter = '</table>';
    document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;       

});

Any help will be appreciated. Thanks in advance.

任何帮助将不胜感激。提前致谢。

采纳答案by Raab

if I could get your point correctly

如果我能正确理解你的观点

DEMO: http://jsfiddle.net/75zFX/8/

演示http: //jsfiddle.net/75zFX/8/

var colheader = '<tr><th>Column1</th><th>Column2</th> .....</tr>\n';
var tbody = colheader ;// and replace with var tbody = '';

回答by Mateusz Rogulski

You can build your header:

您可以构建标题:

var header = "<th>...</th>";

Then:

然后:

$("table").append(header);

回答by Aravinthan K

If u want to add Header Manually to a Table,use this code.

如果您想手动将标题添加到表中,请使用此代码。

JSP

JSP

<th id="example"></th>

JavaScript

JavaScript

$("#example").html("Header Value");