7 Replies Latest reply on Nov 25, 2009 8:25 AM by leonidas113

    Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh

    leonidas113

      i created a Dashboard in which I have a barchart that gets its values from the selected item of the datagrid, the datagrid in itself is getting its data from an external xml file that gets updated every 10 minutes.

       

       

      The Data grid gets updated just fine but it clears the selected item wich in turn clears the barchart.

       

      How can i preserve the selected item of the datagrid after it has been updated with the new data????

       

       

      any help will be greatly apreciated.

        • 1. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
          ShankarFlex3 Level 2

          Hi,

           

          You can store the selectedItem on itemClick event in a Global variable and use it to set after the dataProvider refresh.

          1 person found this helpful
          • 2. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
            leonidas113 Level 1

            could you post some code as to how to do so...

             

             

            Im fairly new to flex and ActionScript

            • 3. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
              ShankarFlex3 Level 2

              Hi,

               

              Attach your code, I will modify it.

              • 4. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
                Prabh Singh Level 1

                [Bindable]
                private var myDP:XML;

                 


                myDP = 'yourDG'.selectedItem as XML;

                 

                //After refresh

                 

                yourDG.selectedItem = <use e4x to get the correct object from the xml structure based on the myDP object>

                1 person found this helpful
                • 5. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
                  leonidas113 Level 1

                  ShankarFlex3 wrote:

                   

                  Hi,

                   

                  Attach your code, I will modify it.

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:Application

                   

                   

                  xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ilog="http://www.ilog.com/2007/ilog/flex" styleName="bluish" backgroundGradientColors="[#055B97, #07A7DB]" backgroundGradientAlphas="[1.0, 1.0]" creationComplete="unitRPC.send();startApp()">

                   

                  <mx:Script>

                  <![CDATA[

                   

                   

                  import mx.events.CollectionEvent;

                   

                  import flash.utils.setInterval;

                   

                  import mx.rpc.events.ResultEvent;

                   

                  import utils.Util;

                   

                  import mx.collections.ArrayCollection;

                   

                  import flash.display.Sprite;

                   

                  import flash.utils.*;

                   

                   

                  private function unitRPCResult(event:ResultEvent):void

                  {

                  units=event.result.root.Interval;

                  total=event.result.root.Interval;

                  }

                  [

                   

                  Bindable]

                   

                  private var units:ArrayCollection=new ArrayCollection();

                  [

                  Bindable]

                   

                  private var total:ArrayCollection=new ArrayCollection();

                   

                  private function changeData(dataSet:String):void

                  {

                   

                   

                  //Determine which set should be loaded

                   

                   

                  switch (dataSet){

                   

                  case ('set1'):

                   

                  //Set URL target to Test Data 1

                  unitRPC.url =

                   

                  "http://192.168.2.11:9090/myfile1.xml";

                   

                  break;

                   

                  case ('set2'):

                   

                  //Set URL target to Test Data 2

                  totalRPC.url =

                   

                  "http://192.168.2.11:9090/totals1.xml";

                   

                  break;

                   

                  default:

                   

                  //If somehow it's neither, just leave it be

                   

                   

                  break;

                  }

                   

                  //Send out new URL Request to refresh chart

                  unitRPC.send();

                  }

                   

                   

                  private var intervalDuration:Number = 10000 // Time in milliseconds

                   

                   

                  private function startApp():void

                  {

                  setInterval(changeData, intervalDuration,

                   

                  'set1');

                  }

                  [

                  Bindable]

                   

                  private var selectedItems:Array;

                  ]]>

                  </mx:Script>

                  <mx:Binding

                   

                   

                  source="dg.selectedItems" destination="selectedItems" />

                   

                  <mx:SeriesInterpolate

                   

                   

                  id="changeEffect" duration="1000"/>

                  <mx:HTTPService

                   

                   

                  id="unitRPC" url=http://192.168.2.11:9090/myfile1.xml result="unitRPCResult(event)"/>

                  <mx:HTTPService

                   

                  id="totalRPC" url="http://192.168.2.11:9090/totals1.xml" result="unitRPCResult(event)"/>

                   

                  <mx:VBox x="151" y="46" width="100%" height="90%" id="vbox4">

                   

                   

                  <mx:Panel width="95%" height="100%" layout="absolute" id="panel1">

                   

                   

                  <mx:ColumnChart x="0" y="0" id="CallChart" width="100%" height="100%" dataProvider="{selectedItems}">

                   

                  <mx:horizontalAxis>

                   

                  <mx:CategoryAxis categoryField="Time"/>

                   

                  </mx:horizontalAxis>

                   

                  <mx:series>

                   

                  <mx:ColumnSeries showDataEffect="changeEffect" displayName="Answered" yField="Answer"/>

                   

                  <mx:ColumnSeries showDataEffect="changeEffect" displayName="Dropped" yField="Dropped"/>

                   

                  <mx:ColumnSeries showDataEffect="changeEffect" displayName="Xfer" yField="Xfer"/>

                   

                  </mx:series>

                   

                  </mx:ColumnChart>

                   

                  <mx:Legend dataProvider="{CallChart}" right="10" top="10" id="legend1"/>

                   

                  </mx:Panel>

                   

                  <mx:DataGrid x="0" y="0" width="95%" height="100%" editable="false" id="dg" dataProvider="{units}" resizableColumns="true">

                   

                  <mx:columns>

                   

                  <mx:DataGridColumn headerText="Interval" dataField="Time"/>

                   

                  <mx:DataGridColumn headerText="Answer" dataField="Answer"/>

                   

                  <mx:DataGridColumn headerText="Dropped" dataField="Dropped"/>

                   

                  <mx:DataGridColumn headerText="Xfer" dataField="Xfer"/>

                   

                  <mx:DataGridColumn headerText="Average Talk Time" dataField="ATT"/>

                   

                  </mx:columns>

                   

                  </mx:DataGrid>

                   

                  </mx:VBox>

                  • 6. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
                    ShankarFlex3 Level 2

                        Hi,

                     

                         All the bold italic font text are changed or added. I hope this should work

                     

                         import mx.events.ListEvent;

                     

                         public var crntObj:Object = new Object();
                       
                        private function unitRPCResult(event:ResultEvent):void
                        {
                            units=event.result.root.Interval;
                            total=event.result.root.Interval;
                           
                            dg.selectedItem = crntObj;
                        }

                     

                        //add a new function

                        private function storeObject(event:ListEvent):void
                        {
                              crntObj = event.currentTarget.selectedItem as Object;
                        }

                     

                    <mx:DataGrid x="0" y="0" width="95%" height="100%" editable="false" id="dg" dataProvider="{units}" itemClick="storeObject(event)" resizableColumns="true">
                        <mx:columns>
                            <mx:DataGridColumn headerText="Interval" dataField="Time"/>
                            <mx:DataGridColumn headerText="Answer" dataField="Answer"/>
                            <mx:DataGridColumn headerText="Dropped" dataField="Dropped"/>
                            <mx:DataGridColumn headerText="Xfer" dataField="Xfer"/>
                            <mx:DataGridColumn headerText="Average Talk Time" dataField="ATT"/>
                        </mx:columns>
                    </mx:DataGrid>

                    • 7. Re: Preserve 'selectedItem' on DataGrid AFTER dataProvider refresh
                      leonidas113 Level 1

                      I get this error message:

                       

                       

                      TypeError: Error #1009: Cannot access a property or method of a null object reference.
                      at KodakDash/unitRPCResult()[C:\Documents and Settings\zwing\My Documents\Flex Builder 3\KodakDash\src\KodakDash.mxml:20]
                      at KodakDash/__unitRPC_result()[C:\Documents and Settings\zwing\My Documents\Flex Builder 3\KodakDash\src\KodakDash.mxml:58]
                      at flash.events::EventDispatcher/dispatchEventFunction()
                      at flash.events::EventDispatcher/dispatchEvent()
                      at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C:\autobuild\3.2.0\framewor ks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290]
                      at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\ projects\rpc\src\mx\rpc\AbstractInvoker.as:193]
                      at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responde r.as:43]
                      at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\ AsyncRequest.as:74]
                      at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\s rc\mx\messaging\channels\DirectHTTPChannel.as:403]
                      at flash.events::EventDispatcher/dispatchEventFunction()

                       

                       

                       

                      And it does not keep the selected item after the datagrid has refreshed