[flexcoders] getting the sum of values in list datafield.
Hello, i have a flex list that is populated by an arraycollection. in the arraycollection i have a field subTotal and i would like to calculate the subtotals from that field and pin them to a text component and i would like to do that even when the list is filtered. below is my code ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical width=100% height=100% verticalAlign=top verticalGap=0 paddingTop=0 paddingBottom=0 paddingLeft=0 paddingRight=0 xmlns:ns1=ascript.* creationComplete=init() initialize=initDate() xmlns:ns2=components.* mx:Style source=css/styleSheet.css/ mx:Script ![CDATA[ import components.newEntryForm; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.managers.CursorManager; import mx.managers.PopUpManager; import mx.rpc.events.ResultEvent; import mx.events.CollectionEvent; protected const MAX_DATE:Date=new Date(2020, 11, 31); /*** code to Filter based on Date Range */ protected const MIN_DATE:Date=new Date(2014, 0, 1); /*** Start Pop Ups */ private var add_win:newEntryForm; /*** code to get Expense Details */ [Bindable] private var expenseAr:ArrayCollection; protected function arrColl_filterFunc(item:Object):Boolean { var cDate:Number=Date.parse(item.hireDate); if (!sDate || !eDate) { return true; } if (sDate.selectedDate eDate.selectedDate) { return (sDate.selectedDate.time = cDate) (eDate.selectedDate.time = cDate); } else if (sDate.selectedDate) { return sDate.selectedDate.time = cDate; } else if (eDate.selectedDate) { return eDate.selectedDate.time = cDate; } else { return true; } //expenseAr.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum); } protected function initDate():void { sDate.selectedDate=MIN_DATE; sDate.selectableRange={rangeStart: MIN_DATE, rangeEnd: MAX_DATE}; eDate.selectedDate=MAX_DATE; eDate.selectableRange=sDate.selectableRange; } private function createdaddPopup():void { add_win=newEntryForm(PopUpManager.createPopUp(this, newEntryForm, true)); } private function expenseResult(event:ResultEvent):void { expenseAr=event.result as ArrayCollection; expenseAr.filterFunction=arrColl_filterFunc; expenseAr.refresh(); } private function init():void { moRentalsSvc.getExpense(); //expenseAr.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum); } public function calculateSum(event:CollectionEvent):void { var amt:Number=0; var n:int=expenseAr.length; for (var i:int=0; i n; i++) { var expenseEntry:Object=expenseAr.getItemAt(i); amt+=expenseEntry.subTotal; } sum.text=ugxftr.format(amt.toString()); } ]] /mx:Script mx:RemoteObject id=moRentalsSvc destination=ColdFusion source=moRentals.src.CFCs.crud showBusyCursor=true fault=CursorManager.removeBusyCursor();Alert.show(event.fault.message) mx:method name=getExpense result=expenseResult(event)/ /mx:RemoteObject mx:CurrencyFormatter id=ugxftr precision=0 thousandsSeparatorTo=, useThousandsSeparator=true rounding=up currencySymbol=UGX / mx:DateFormatter id=dateFormat formatString= DD, / mx:HBox styleName=header width=100% height=50 verticalAlign=middle verticalGap=0 mx:Image source=assets/logo.png/ mx:Spacer width=100%/
[flexcoders] Re: getting the sum of values in list datafield.
Any Help Guys?
[flexcoders] Re: getting the sum of values in list datafield.
hmm seriously i have hit a wall. could use an example
Re: [flexcoders] getting the sum of values in list datafield.
The code looks mostly ok. I noticed you commented out the addEventListener calls for COLLECTION_CHANGE. I'll bet it threw an error becuae expenseAr is null until the RemoteObject returns a result. I would move the addEventLIstener call to expenseResult() method when you know expenseAr is valid. -Alex From: stinas...@yahoo.commailto:stinas...@yahoo.com [flexcoders] flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com Reply-To: flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com Date: Friday, August 29, 2014 1:55 AM To: flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com Subject: [flexcoders] getting the sum of values in list datafield. Hello, i have a flex list that is populated by an arraycollection. in the arraycollection i have a field subTotal and i would like to calculate the subtotals from that field and pin them to a text component and i would like to do that even when the list is filtered. below is my code ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical width=100% height=100% verticalAlign=top verticalGap=0 n! bsp; paddingTop=0 paddingBottom=0 paddingLeft=0 paddingRight=0 xmlns:ns1=ascript.* creationComplete=init() initialize=initDate() xmlns:ns2=components.* mx:Style source=css/styleSheet.css/ mx:Script nbs! p; ![CDATA[ !nbsp; import components.newEntryForm; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.managers.CursorManager; import mx.managers.PopUpManager; import mx.rpc.events.ResultEvent; import mx.events.CollectionEvent; protected const MAX_DATE:Date=new Date(2020, 11, 31); /*** code to Filter based on Date Range */ protected const MIN_DATE:Da! te=new Date(2014, 0, 1); /*** Start Pop Ups */ private var add_win:newEntryForm; /*** code to get Expense Details */ [Bindable] private var expenseAr:ArrayCollection; protected function arrColl_filterFunc(item:Object):Boolean { var cDate:Number=Date.parse(item.hireDate); ! ; if (!sDate || !eDate) nbs! p; { return true; } if (sDate.selectedDate eDate.selectedDate) { return (sDate.selectedDate.time = cDate) (eDate.selectedDate.time = cDate); } else if (sDate.selectedDate) { nb! sp; return sDate.selectedDate.time = cDate; } else if (eDate.selectedDate) { return eDate.selectedDate.time = cDate; } else { return true; } ! ; //expenseAr.addEventListener(Collec! tionEven t.COLLECTION_CHANGE, calculateSum); } protected function initDate():void { sDate.selectedDate=MIN_DATE; sDate.selectableRange={rangeStart: MIN_DATE, rangeEnd: MAX_DATE}; eDate.selectedDate=MAX_DATE; eDate.selectableRange=sDate.selectableRange; } private function createdaddPopup():void { nbs! p; add_win=newEntryForm(PopUpManager.createPopUp(this, newEntryForm, true)); } private function expenseResult(event:ResultEvent):void { expenseAr=event.result as ArrayCollection; expenseAr.filterFunction=arrColl_filterFunc; expenseAr.refresh(); } private function init():void { ! nbsp; moRentalsSvc.getExpense()! ; nb sp; //expenseAr.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum); } public function calculateSum(event:CollectionEvent):void { var amt:Number=0; var n:int=expenseAr.length; for (var i:int=0; i n; i++) { var expenseEntry:Object=expenseAr.getItemAt(i); nbs! p; amt+=expenseEntry.subTotal; } sum.text=ugxftr.format(amt.toString()); } ]] /mx:Script mx:RemoteObject id=moRentalsSvc
Re: [flexcoders] getting the sum of values in list datafield.
IMHO - you should create a separate bindable string and set the value to that instead of directly setting the .text property of a text field. Your reduce function seems ok - just run each object through the filter function to see if it returns true. Only objects that return true should be added to the sum. You are 99% of the way there. What is tripping you up? Sent from my iPhone On Aug 29, 2014, at 4:55 AM, stinas...@yahoo.com [flexcoders] flexcoders@yahoogroups.com wrote: Hello, i have a flex list that is populated by an arraycollection. in the arraycollection i have a field subTotal and i would like to calculate the subtotals from that field and pin them to a text component and i would like to do that even when the list is filtered. below is my code ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical width=100% height=100% verticalAlign=top verticalGap=0 paddingTop=0 paddingBottom=0 paddingLeft=0 paddingRight=0 xmlns:ns1=ascript.* creationComplete=init() initialize=initDate() xmlns:ns2=components.* mx:Style source=css/styleSheet.css/ mx:Script ![CDATA[ import components.newEntryForm; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.managers.CursorManager; import mx.managers.PopUpManager; import mx.rpc.events.ResultEvent; import mx.events.CollectionEvent; protected const MAX_DATE:Date=new Date(2020, 11, 31); /*** code to Filter based on Date Range */ protected const MIN_DATE:Date=new Date(2014, 0, 1); /*** Start Pop Ups */ private var add_win:newEntryForm; /*** code to get Expense Details */ [Bindable] private var expenseAr:ArrayCollection; protected function arrColl_filterFunc(item:Object):Boolean { var cDate:Number=Date.parse(item.hireDate); if (!sDate || !eDate) { return true; } if (sDate.selectedDate eDate.selectedDate) { return (sDate.selectedDate.time = cDate) (eDate.selectedDate.time = cDate); } else if (sDate.selectedDate) { return sDate.selectedDate.time = cDate; } else if (eDate.selectedDate) { return eDate.selectedDate.time = cDate; } else { return true; } //expenseAr.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum); } protected function initDate():void { sDate.selectedDate=MIN_DATE; sDate.selectableRange={rangeStart: MIN_DATE, rangeEnd: MAX_DATE}; eDate.selectedDate=MAX_DATE; eDate.selectableRange=sDate.selectableRange; } private function createdaddPopup():void { add_win=newEntryForm(PopUpManager.createPopUp(this, newEntryForm, true)); } private function expenseResult(event:ResultEvent):void { expenseAr=event.result as ArrayCollection; expenseAr.filterFunction=arrColl_filterFunc; expenseAr.refresh(); } private function init():void { moRentalsSvc.getExpense(); //expenseAr.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum); } public function calculateSum(event:CollectionEvent):void { var amt:Number=0; var n:int=expenseAr.length; for (var i:int=0; i n; i++) { var expenseEntry:Object=expenseAr.getItemAt(i); amt+=expenseEntry.subTotal; } sum.text=ugxftr.format(amt.toString()); } ]] /mx:Script mx:RemoteObject id=moRentalsSvc destination=ColdFusion source=moRentals.src.CFCs.crud showBusyCursor=true fault=CursorManager.removeBusyCursor();Alert.show(event.fault.message) mx:method name=getExpense