组表行数组

时间:2011-09-18 20:12:06

标签: jquery html-table

我需要将可变数量的表行分组到一个数组中。每组有5个单元格,如HTML所示:

<tr> 
<td width='23%' class='table-item-1'><strong>EVP</strong></td> 
<td width='15%' class='table-item-1'>Diane</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 
<tr> 
<td width='23%' class='table-item-1'><strong>EVP</strong></td> 
<td width='15%' class='table-item-1'>Leslee</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 
<tr> 
<td width='23%' class='table-item-1'><strong>Production Mgr</strong></td> 
<td width='15%' class='table-item-1'><s>Lenny  </s>&nbsp; tbh</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 
<tr> 
<td width='23%' class='table-item-1'><strong>Production</strong></td> 
<td width='15%' class='table-item-1'>Jessica</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-2xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr>

这是我到目前为止所做的:

var items_size = $('[width|="23%"]').size(); //gives me the amount of groups

var items1 = {   
        'name': $('.table-item-1 ~ td').html(),
        'email': $('.table-item-1 ~ td > a').html(),
        'phone': $('.table-item-1 ~ td').next().html(),
        'fax': $('.table-item-1 ~ td').next().next().html(),
        'title': $('.table-item-1 ~ td').prev().children().html()
        }; // creates an array containing the items from the first group.

我需要为HTML中的每个组创建相同的数组。

1 个答案:

答案 0 :(得分:1)

var items = [];
$('table tr').each(function () {
  // access each tr's tds from here using $(this)
  var item = {
       'name' : $(this).find('td:first').text(),
       ...
  }
  items.push(item);
});