Gopesh
Gopesh

Reputation: 3950

Find the column Sum of table using jquery

I am new to this forum. I need to get the product of rows and sum of columns.Row product is ok now.but it seems very difficult to find the column sum Here is the LInk http://jsfiddle.net/Mpayr/10/ Please help me to solve this??

Upvotes: 0

Views: 11784

Answers (3)

sayannayas
sayannayas

Reputation: 774

If you are looking for summation in both x and y pattern like row total & column total you can check my example excel like sum jquery

Upvotes: 1

rjralgar
rjralgar

Reputation: 153

Try using classes for rows and sum iteratively over the class:

<table>
    <th>First</th>
    <th>Second</th>
    <th>Third</th>
    <th>SUM</th>
    <tr>
        <td><input class="row1 column1" value = "100"/></td>
        <td><input class="row1 column2" value = "100"/></td>
        <td><input class="row1 column3" value = "100"/></td>
        <td class="total">300</td>
     </tr>
     <tr>
        <td><input class="row2 column1" value = "200"/></td>
        <td><input class="row2 column2" value = "200"/></td>
        <td><input class="row2 column3" value = "200"/></td>
        <td class="total">600</td>
     </tr>
     <tr><td>Sum of Column(SUM)</td><td><input name="result" id="result"></td></tr>
</table>

with

$(document).ready(function(){
    $(this).keyup(function(){ 
        var sum = 0;
        var column2 = $('.column2')
        jQuery.each(column2,function(number){
            sum += parseInt($(this).val());
        });
        $('#result').val(sum);
    });
})

.each then allows you to sum over all the inputs with this class. Use this approach for each of the columns and rows.

Upvotes: 4

bondythegreat
bondythegreat

Reputation: 1409

i've update your codes here http://jsfiddle.net/bondythegreat/dm4AZ/1/

Upvotes: 2

Related Questions