0

I would like to do statistics in flex. I have recuperated my array list from the java service when i clicked on a button and i recuperate the end and finish date of statistics when i test the return values of the list in java it gives the correct the result but in flex the datagrid is not filled.
This is the method in flex:

[Bindable]
private var  FinalResult:ArrayCollection = new ArrayCollection(); 

private function getTicketByStatus(evt:ResultEvent):void
        {   
            FinalResult= evt.result as ArrayCollection;
            dg.dataProvider=FinalResult;
        }  
protected function buttnSta_clickHandler(event:MouseEvent):void
        {
            startDate=StartDateField.selectedDate;
            endDate=EndDateField.selectedDate;
            CountTicketsByStatusResult.token = ticketServiceImpl.CountTicketsByStatus(startDate,endDate);
            FinalResult=CountTicketsByStatusResult.lastResult;
            dg.dataProvider=FinalResult;
            for(var i=0;i<FinalResult.length;i++)
            {
                Alert.show("element"+FinalResult.length.toString());
            }
         }

<mx:DataGrid id="dg" x="306" y="91" width="354" height="208" dataProvider="{FinalResult}">
Dan
  • 59,490
  • 13
  • 101
  • 110
hana
  • 1
  • 4

2 Answers2

1

The service call you're invoking is asynchronous. This means that you cannot take the result from "lastResult" right after you invoke the service. You need to add a responder to the token that is returned.

Also, since you are already binding to the FinalResult arraycollection on the datagrid, you don't need to set it again in the getTicketByStatus method.

A last thing to mention: private variables are prefixed with an underscore by convention and start with a lowercase letter. So FinalResult becomes _finalResult.

Your code should look something like this:

[Bindable]
private var _finalResult:ArrayCollection = new ArrayCollection(); 

private function getTicketByStatus(evt:ResultEvent):void
    {   
        _finalResult = evt.result as ArrayCollection;
    } 

private function getTicket_faultHandler(evt:FaultEvent):void
    {   
        // error
    } 

protected function buttnSta_clickHandler(event:MouseEvent):void
    {
        startDate=StartDateField.selectedDate;
        endDate=EndDateField.selectedDate;
        var token:AsyncToken = ticketServiceImpl.CountTicketsByStatus(startDate,endDate);
        token.addResponder(new Responder(getTicketByStatus, getTicket_faultHandler));
     }

Christophe Herreman
  • 15,895
  • 9
  • 58
  • 86
  • I'm not sure the underscore convention has been adopted by the entire community for purely private variables (i.e. the ones that don't have any getters/setters); it's not being practiced in the Flex source code anyway. @hana Maybe one more thing to point out: your code would've worked if you had bound directly to `CountTicketsByStatusResult.lastResult` (assuming `CountTicketsByStatusResult` is Bindable). When the data comes in, a PropertyChangeEvent is fired for `lastResult` and the binding updates the grid's dataProvider. – RIAstar May 25 '13 at 11:27
  • Mosts likely not indeed. I added it since I *assume* most Flex developers use this practive although it is of course subjective. – Christophe Herreman May 25 '13 at 12:28
  • I don't. I have this irrational aversion towards everything that is not a letter in a variable/method/class name. Something like `buttnSta_clickHandler` just makes me cringe, more because of `buttn` and `Sta` (what on earth is a 'sta'? and why the hell would one write 'buttn' instead of 'button'?) than the `_` but still (for the record: I know you just copied that). Can I no longer apply for a job with you know? ;p From a practical POV: when I see `_myVar = "someVal"` in my code, I instantly know that I explicitly avoided the setter, which I couldn't if every private variable had an underscore – RIAstar May 25 '13 at 13:38
0

i had added alerts to test my code but when i click on the button the view appears like bloqued this is the new code and there is no alert that appear

[Bindable]
            var startDate:Date;
            [Bindable]
            var endDate:Date;
 [Bindable]
            private var  final_result:ArrayCollection; 
public function getTicketByStatus(evt:ResultEvent):void
            {   Alert.show("1");
                final_result= evt.result as ArrayCollection;
                dg.dataProvider=final_result;   
            } 
    protected function button1_clickHandler(event:MouseEvent):void
            {
                startDate=StartDateField.selectedDate;
                endDate=EndDateField.selectedDate;
                CountTicketsByStatusResult.addEventListener(ResultEvent.RESULT,getTicketByStatus);
                CountTicketsByStatusResult.addEventListener(FaultEvent.FAULT, onFault_handler);
                CountTicketsByStatusResult.token = ticketServiceImpl.CountTicketsByStatus(startDate,endDate);
            }
            protected function onFault_handler(event:FaultEvent):void {
                Alert.show("Error in calling service: " + event.message, "Error");
            }
<s:NavigatorContent width="100%" height="100%" label="Tickets By status">
            <mx:DateField id="StartDateField" x="112" y="10" width="151"/>
            <mx:DateField id="EndDateField"  x="803" y="7" width="153" />
            <s:Label x="17" y="9" width="61" height="22" fontFamily="Georgia" fontWeight="bold"
                     text="From"/>
            <s:Label x="769" y="17" width="40" height="22" fontFamily="Georgia" fontWeight="bold"
                     text="To"/>


            <mx:PieChart id="myChart" x="288" y="355" height="212" dataProvider="{final_result}" showDataTips="true">
                <mx:series>
                    <mx:PieSeries 
                        field="number" 
                        nameField="Status" 
                        labelPosition="callout"/>
                </mx:series>
            </mx:PieChart>
            <mx:Legend x="41" y="372" width="200" height="188" dataProvider="{final_result}"/>
            <mx:DataGrid id="dg" x="293" y="153" width="420" height="189" dataProvider="{final_result}">
                <mx:columns>
                    <mx:DataGridColumn dataField="status" headerText="Metrics"/>
                    <mx:DataGridColumn dataField="number" headerText="Number" />
                </mx:columns>
            </mx:DataGrid>
            <s:Button x="434" y="61" width="133" label="Generate Statics"
                      click="button1_clickHandler(event)"/>


        </s:NavigatorContent>
hana
  • 1
  • 4