Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtotal 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 have to subtotal group. means in above example Black have 2 Child and White have 3 child

like image 842
John Avatar asked Jun 07 '26 12:06

John


1 Answers

Change the id='trGroup' to class='trGroup' (you're supposed to have unique id's), and then use the nextUntil method.

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

DEMO

like image 190
tewathia Avatar answered Jun 10 '26 03:06

tewathia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!