最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add header for dynamic table - Stack Overflow

programmeradmin5浏览0评论

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('.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.

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./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.

Share Improve this question edited Jun 16, 2012 at 10:47 Govind Malviya 13.8k17 gold badges69 silver badges95 bronze badges asked Jun 16, 2012 at 10:46 PPDPPD 5,90013 gold badges54 silver badges89 bronze badges 4
  • 2 Are you talking about <th> in table or something else – Rab Commented Jun 16, 2012 at 10:51
  • @ CoDe Yes I mean <th>. But when I try to use it I got error. – PPD Commented Jun 16, 2012 at 10:52
  • @PramodD What kind of error? And using what browser? Bu, above all, what did you actually try? – MaxArt Commented Jun 16, 2012 at 10:54
  • Here jsfiddle/75zFX/6 what I had tried But if I remove th then table is visible – PPD Commented Jun 16, 2012 at 11:05
Add a ment  | 

3 Answers 3

Reset to default 2

You can build your header:

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

Then:

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

if I could get your point correctly

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

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

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

JSP

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

JavaScript

$("#example").html("Header Value"); 
发布评论

评论列表(0)

  1. 暂无评论