2012-06-16 28 views
1

大家好,我繪製動態表,我想添加標題爲表如何頭部添加到這裏動態表是我的表碼:如何添加頁眉動態表

$.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;  

}); 

任何幫助將不勝感激。提前致謝。

+0

@Govind感謝後期編輯 – PPD

+2

你在表或別的東西 – Rab

+0

談論'' @代碼是我的意思是。但是當我嘗試使用它時,我得到了錯誤。 – PPD

回答

1

如果我能得到你的觀點正確

DEMOhttp://jsfiddle.net/75zFX/8/

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

在這裏,我嘗試http://jsfiddle.net/pramodD/75zFX/7/,但它不起作用。 – PPD

+1

你用錯了。看到這個http://jsfiddle.net/75zFX/8/ – Rab

+0

@ COde感謝它的完美。謝謝 – PPD

1

你可以建立你的頭:

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

然後:

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

@ Mateusz謝謝你的迴應。 – PPD

0

如果你想手動添加頭到表中,使用這個代碼。

JSP

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

的JavaScript

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