Black, Total

Summary group using jquery

Fiddle

<table border="1" class="cssTable">
    <tr id="trGroup">
        <td>
            Black, Total <asp:Label ID="lblCount" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="cssTd">
              A            
         </td>
    </tr>   
    <tr>
        <td class="cssTd">
              B            
         </td>
    </tr>   
    <tr id="trGroup">
        <td>
            White, Total <asp:Label ID="lblCount" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="cssTd">
              X            
         </td>
    </tr>   
    <tr>
        <td class="cssTd">
              Y            
         </td>
    </tr>   
    <tr>
        <td class="cssTd">
             Z       
         </td>
    </tr>   
    </table>

I must have an intermediate group. means that in the above example black has 2 children and white has 3 children

+3
source share
2 answers

Change id='trGroup'to class='trGroup'(you must have a unique identifier) ​​and then use the nextUntil method .

$('.trGroup').each(function () {
    var $this = $(this),
        subTotal = $this.nextUntil('.trGroup').length;
    $this.text($this.text() + subTotal);
});

Demo

+3
source
$('#trGroup').each(function() {
  var len;

  len = $(this).nextUntil('tr#trGroup').length

  // i think this **len** is what you require
})
+1
source

All Articles