对不起(我不能写好英语!),我想获得每个类别的计数产品总和并附加到类别列。 我如何得到每个tr的产品数量总和并附加到本CODE中的当前tr?
$(function() {
function groupTable($rows, startIndex, total) {
if (total === 0) {
return;
}
var i, currentIndex = startIndex,
count = 1,
lst = [];
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++) {
if (ctrl.text() == $(tds[i]).text()) {
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);
} else {
if (count > 1) {
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)
}
count = 1;
lst = [];
ctrl = $(tds[i]);
lst.push($rows[i]);
}
}
}
groupTable($('#myTable tr:has(td)'), 0, 3);
$('#myTable .deleted').remove();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<table id="myTable" border="1" cellpadding="3" cellspacing="0">
<tr>
<th>Category</th>
<th>Product</th>
</tr>
<tr>
<td>Category-1</td>
<td>Product-1</td>
</tr>
<tr>
<td>Category-1</td>
<td>Product-1</td>
</tr>
<tr>
<td>Category-1</td>
<td>Product-2</td>
</tr>
<tr>
<td>Category-1</td>
<td>Product-2</td>
</tr>
<tr>
<td>Category-2</td>
<td>Product-3</td>
</tr>
<tr>
<td>Category-2</td>
<td>Product-3</td>
</tr>
<tr>
<td>Category-2</td>
<td>Product-3</td>
</tr>
<tr>
<td>Category-2</td>
<td>Product-4</td>
</tr>
<tr>
<td>Category-3</td>
<td>Product-5</td>
</tr>
<tr>
<td>Category-3</td>
<td>Product-5</td>
</tr>
<tr>
<td>Category-3</td>
<td>Product-5</td>
</tr>
<tr>
<td>Category-4</td>
<td>Product-6</td>
</tr>
<tr>
<td>Category-4</td>
<td>Product-6</td>
</tr>
</table>
&#13;