Solved

How do I calculate running totals and have them displayed in a flex 3 datagrid?

Posted on 2009-04-03
9
385 Views
Last Modified: 2013-11-11
I am looking for something similar to this.
The function I have now returns Overtime for every day on the overtime data field.  I am thinking I need a running total column so I can specify when overtime should be calculated. I need this to calc similar to the data below.
Date                      Total Hours For Day  Running Total Hours      Wage       Regular Pay  Overtime Pay
2009-03-01           15                                  15                               $16.00         $240.00             $0.00
2009-03-02           15                                  30                               $16.00         $240.00             $0.00
2009-03-03           15                                  45                               $16.00         $160.00             $200.00
Total Hours           45.00


I am doing something like this now but it returns nothing.

//running total function
              private function runningTotal(item : Object, column : DataGridColumn ) : String {
                   var nf : NumberFormatter = new NumberFormatter();  
                   nf.precision = 2;
                   var totalb:Number = 0;
                 for(var i:int = 0; i < ac.length; i++)
{
                 totalb += ac.getItemAt(i).total * 1;
}
                 return nf.format(totalb);  
            }

This works ok but is calculating for everyday when totalhoursworked is > 40.01.
//overtime function
private var _overtime:Number = 0;
 private function getOvertime(item : Object, column : DataGridColumn ) : String {
           if(totalhoursworked > 40.01){
           var total:Number = item.totaltime;//dg item
           var grandtotal:Number= total * overtime;
           _overtime = grandtotal;
             
           }
       else {
         _overtime = 0;
    }
0
Comment
Question by:aduvall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 24059638
I really don't understand your requirements

>> The function I have now returns Overtime for every day on the overtime data field.
So how does the datagrid looks like currently?

>> I am thinking I need a running total column so I can specify when overtime should be calculated.
>> This works ok but is calculating for everyday when totalhoursworked is > 40.01.
What's wrong with that?
When should it be calculated and when not?
0
 

Author Comment

by:aduvall
ID: 24059797
Overtime should only be calculated when the hours worked reach 40.01.
So let's say on day 1 they worked 10 hours
and day 2 they worked 10 hours
and on day 3 they worked 10 hours
on day 4  they worked 12 hours.
Total hours worked 42.
Only 2 hours should be calculated as overtime because they reached 40.01 on day 4 with 2 hours left over for overtime.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 24059974
OK.
And a datagrid like this is (where you add an extra summing/totaling item) not OK for you?

Date                 Total Hours For Day  Overtime
----------------------------------------------
2009-03-01           10                      0
2009-03-02           10                      0
2009-03-03           10                      0      
2009-03-04           12                      0
Total Hours           42                       2             <<==== a summing row you add
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:aduvall
ID: 24060118
A datagrid like that is ok.  But I just do not know how to arrive at that point.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 24060259
>> A datagrid like that is ok.
Fine.

>> I just do not know how to arrive at that point.
The method you have is OK. (except I don't understand why you use > 40.01 and not just 40.0)
For the 4 first items it won't calculate anything and return just 0.
Based on the four items, you have to create a new one and add that to the arraycollection that is the dataprovider of your datagrid.
Creating the new one you have to take care that the total hours is the total of the total hours of the 4 other items.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 24060610
Here's a quick little sample demo app
(I simply use Object as items in the datagrid, but you can use your own object having the methods they have now)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	
	    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
                  
            [Bindable]
            private var _data:ArrayCollection = new ArrayCollection([
            	{ date:"day 1", workingTime:10, overTime:0 },
            	{ date:"day 2", workingTime:10, overTime:0 },
            	{ date:"day 3", workingTime:10, overTime:0 },
            	{ date:"day 4", workingTime:12, overTime:0 }
            ]);
            
            private function onInitDG():void {
            	var total:int = 0;
            	for each (var item:Object in _data) {
            		total += item.workingTime;
            	}
            	var o:Object = new Object();
            	o.date = "Total Hours";
            	o.workingTime = total;
            	o.overTime = total<40 ? 0 : total-40;
            	_data.addItem(o);
            	myDG.dataProvider = _data;
            }
            
        ]]>
    </mx:Script>
            
    <mx:Panel title="Test"
        height="75%" width="75%" layout="horizontal"
        paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
        
        <mx:DataGrid id="myDG" width="100%" height="100%" 
            initialize="onInitDG();">        
            <mx:columns>
                <mx:DataGridColumn dataField="date" />
                <mx:DataGridColumn dataField="workingTime"/>
                <mx:DataGridColumn dataField="overTime"/>
            </mx:columns>
       </mx:DataGrid>
	</mx:Panel>
 
</mx:Application>

Open in new window

0
 

Author Comment

by:aduvall
ID: 24060686
That is pefect. This is what I was looking for.
0
 

Author Closing Comment

by:aduvall
ID: 31566227
Thank You!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 24060816
Glad to hear. Have a nice weekend.
Thanx 4 axxepting
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Flash doesn't load in Iphone and Ipad 22 503
Adobe Flash Free Transform Tool 2 156
Converting CFGRID from Flash to HTML in Coldfusion 1 110
Indesign Data Merge 1 239
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question