2012-12-07 30 views
0

我從HTML表格中提取數據。我已經分居標題和TR數據轉換成2列, 假設我有表:jQuery在提取的表格數據中添加行分隔符

<table cellpadding="0" cellspacing="0" border="0" class="display dTable messageinbox"> 
    <thead> 
     <tr> 
      <th>ID</th> 
      <th align="left">Name</th> 
      <th align="left">Message</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td align="center" style="vertical-align:middle">1</td> 
      <td align="left" style="vertical-align:middle">Ashfaq </td> 
      <td align="left" style="vertical-align:middle">hi </td> 
     </tr> 
     <tr> 
      <td align="center" style="vertical-align:middle">2</td> 
      <td align="left" style="vertical-align:middle">Adam </td> 
      <td align="left" style="vertical-align:middle">test </td> 
     </tr> 
    </tbody> 
</table> 

這裏是我的代碼:

$("#ExportOption").click(function() 
    { 
     var $table = $("table:first"), 
     $headerCells = $table.find("thead th"), 
     $rows = $table.find("tbody tr"); 
     var headers = [], 
      rows = []; 

     $headerCells.each(function(k,v) 
     { 
      headers[headers.length] = $(this).text().replace(',', ''); 
     }); 

     $rows.each(function(row,v) 
     { 
      $(this).find("td").each(function(cell,v) 
      { 
       if(typeof rows[cell] === 'undefined') rows[cell] = []; 
       //alert($(this).parent().find("tr")); 
       rows[row][cell] = $(this).text().replace(',', ''); 
      }); 
     }); 
     alert(headers); 
     alert(rows); 
    }); 

這給我輸出這樣

Heading : 
    ID Name Message 

    Rows : 
    1  Ashfaq hi  2 Adam test 

的我想要的輸出是在每個tr數據之間添加一個行分隔符,如

1 Ashfaq hi \Separator 2 Adam test 

回答

0
$rows.each(function(row, v) { 
    $(this).find("td").each(function(cell, v) { 
     if (typeof rows[cell] === 'undefined') rows[cell] = []; 
     //alert($(this).parent().find("tr")); 
     rows[row][cell] = $(this).text().replace(',', ''); 
    }); 
    rows[row] += "/"; 
}); 

http://jsfiddle.net/BNN6Z/7/

+0

謝謝你工作得很好 –

相關問題