sum of table rows

View: New views
11 Messages — Rating Filter:   Alert me  

sum of table rows

by pedalpete :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


really close to this one, but it seems I'm not looping properly

What I have is a table, and I'm trying to sum just the row values
individually.

the table row has the id of newPerson,
and then the cells have an class of length and the id is the numbers
that need to be added.

but apparently the code below is summing all the id values where the
class="length", not one row at a time.
Anybody know how to get this to seperate them so that I get a seperate
value for each "newPerson' id?

[code]
$("tr#newPerson").each( function(){
                var start=0;
                var totalLength = parseInt(start);
                $(".length").each( function(){
                        var newLength = parseInt(this.id);
                        totalLength = newLength + totalLength;
                        alert(totalLength);

                });
        });
[/code]

Re: sum of table rows

by pedalpete :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm not sure if I am getting closer on this or further away.

I have a table that looks like this
[code]
<table>
   <tr id=newPerson>
          <td class="person">first person</td>
          <td class="date"><div class="input"><span class="length>8</
span></div></td>
          <td class="date"><div class="input"><span class="length>8</
span></div></td>
           <td class="date"><div class="input"><span class="length>8</
span></div></td>
  </tr>
     <tr id=newPerson>
          <td class="person">second person</td>
          <td class="date"><div class="input"><span class="length>8</
span></div></td>
          <td class="date"><div class="input"><span class="length>8</
span></div></td>
           <td class="date"><div class="input"><span class="length>8</
span></div></td>
  </tr>
     <tr id=newPerson>
          <td class="person">third person</td>
          <td class="date"><div class="input"><span class="length>8</
span></div></td>
          <td class="date"><div class="input"><span class="length>8</
span></div></td>
           <td class="date"><div class="input"><span class="length>8</
span></div></td>
  </tr>
</table>
[/code]

and i am trying to get the sum of the content of the .length values
for each person seperately.

I have now got this, but I don't seem to be getting anywhere, now I'm
not even able to get the values.
Am I not going about getting the lengths properly?

[code]
var start = 0;
                var totalLength = parseInt(start);
                ("#newPerson").click(function(event){
                var $newPerson = $("#newPerson").children( function(){
                        var len = $newPerson.each(".shift").id;
                        alert(len);
                });
                });
[/code]




Re: sum of table rows

by Dan G. Switzer, II :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


It sounds as if this might be a good candidate for my Calculation plug-in:
http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm

$("tr").each(function (){
        var sum = $("span.length", this).sum();
        var person = $("td.person").text();

        alert(person + " = " + sum);
});

-Dan

>-----Original Message-----
>From: jquery-en@... [mailto:jquery-en@...] On
>Behalf Of pedalpete
>Sent: Tuesday, January 22, 2008 10:13 PM
>To: jQuery (English)
>Subject: [jQuery] Re: sum of table rows
>
>
>I'm not sure if I am getting closer on this or further away.
>
>I have a table that looks like this
>[code]
><table>
>   <tr id=newPerson>
>          <td class="person">first person</td>
>          <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>          <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>           <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>  </tr>
>     <tr id=newPerson>
>          <td class="person">second person</td>
>          <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>          <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>           <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>  </tr>
>     <tr id=newPerson>
>          <td class="person">third person</td>
>          <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>          <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>           <td class="date"><div class="input"><span class="length>8</
>span></div></td>
>  </tr>
></table>
>[/code]
>
>and i am trying to get the sum of the content of the .length values
>for each person seperately.
>
>I have now got this, but I don't seem to be getting anywhere, now I'm
>not even able to get the values.
>Am I not going about getting the lengths properly?
>
>[code]
>var start = 0;
>                var totalLength = parseInt(start);
> ("#newPerson").click(function(event){
> var $newPerson = $("#newPerson").children( function(){
> var len = $newPerson.each(".shift").id;
> alert(len);
> });
> });
>[/code]
>
>



Re: sum of table rows

by rolfsf :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's a nice little plugin Dan! Is it possible to sum a column of text inputs as they're filled (self-totalling, rather than onClick)?

Rolf


Dan G. Switzer, II wrote:
It sounds as if this might be a good candidate for my Calculation plug-in:
http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm

$("tr").each(function (){
        var sum = $("span.length", this).sum();
        var person = $("td.person").text();

        alert(person + " = " + sum);
});

-Dan

Re: sum of table rows

by Dan G. Switzer, II :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Rolf,

>That's a nice little plugin Dan! Is it possible to sum a column of text
>inputs as they're filled (self-totalling, rather than onClick)?

Sure! You'd just need to trigger sum() event to occur on a keypress event.
If you're going to do that, I'd definitely recommend caching the jQuery
object you're going to perform the sum() on (that way you're not spending
cycles always looking up which elements to sum().)

One neat feature is that you can mix and match element types with the
mathematical functions. So you can sum a text element against a div element.

I just realized the description text on the page is completely wrong! It's
for another plug-in and I used that page as a template for this one. :)

-Dan

>
>
>Dan G. Switzer, II wrote:
>>
>>
>> It sounds as if this might be a good candidate for my Calculation plug-
>in:
>> http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm
>>
>> $("tr").each(function (){
>> var sum = $("span.length", this).sum();
>> var person = $("td.person").text();
>>
>> alert(person + " = " + sum);
>> });
>>
>> -Dan
>>
>>
>
>--
>View this message in context: http://www.nabble.com/sum-of-table-rows-
>tp14879031s27240p15050352.html
>Sent from the jQuery General Discussion mailing list archive at Nabble.com.



Re: sum of table rows

by Dan G. Switzer, II :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


>I just realized the description text on the page is completely wrong! It's
>for another plug-in and I used that page as a template for this one. :)

I just updated the Calculation Plug-in page so that the description is
accurate. I also updated the examples so they work when the numbers are
changed (no need to hit the "Calc" buttons.)

http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm

-Dan


Re: sum of table rows

by Jonathan Sharp-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey Dan,
 
 
Cheers,
-Jonathan

 
On 1/23/08, Dan G. Switzer, II <dswitzer@...> wrote:

>I just realized the description text on the page is completely wrong! It's
>for another plug-in and I used that page as a template for this one. :)

I just updated the Calculation Plug-in page so that the description is
accurate. I also updated the examples so they work when the numbers are
changed (no need to hit the "Calc" buttons.)

http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm

-Dan



Re: sum of table rows

by rolfsf :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sweet! Thanks man!


Dan G. Switzer, II wrote:
>I just realized the description text on the page is completely wrong! It's
>for another plug-in and I used that page as a template for this one. :)

I just updated the Calculation Plug-in page so that the description is
accurate. I also updated the examples so they work when the numbers are
changed (no need to hit the "Calc" buttons.)

http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm

-Dan

Re: sum of table rows

by Dan G. Switzer, II :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Jonathan,

>Hey Dan,
>
>Great plugin! http://jqueryminute.com/blog/jquery-calculate-plugin/

Thanks! I also blogged a little more information here:
http://blog.pengoworks.com/index.cfm/2008/1/23/jQuery-Calculation-Plugin-Mak
ing-calculating-easy

-Dan


Re: sum of table rows

by Jonathan Sharp-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, updated the entry with a link to that post!
 
Cheers,
-Jonathan

 
On 1/23/08, Dan G. Switzer, II <dswitzer@...> wrote:

Jonathan,

>Hey Dan,
>
>Great plugin! http://jqueryminute.com/blog/jquery-calculate-plugin/

Thanks! I also blogged a little more information here:
http://blog.pengoworks.com/index.cfm/2008/1/23/jQuery-Calculation-Plugin-Mak
ing-calculating-easy

-Dan



Re: sum of table rows

by pedalpete :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


that AWESOME!!
thanks Dan,

I had looked at your plugin before, but thought it was overkill for
what I was trying to do (I'm fairly new to Javascript and quite new to
jquery as well).

Thanks for including the bit of code I needed to get your plug-in
working for my site. I don't know that I would have gotten it without
your help.

Pete