11 Replies Latest reply on Oct 8, 2010 5:32 AM by Aar0nm

    datagrid set verticalScroll after data is updated - What does it take?

    Aar0nm

      Hello,

       

      Something fundamental about datagrid and verticalScroll I must be missing.

      Here's my setup hoping someone can help me understand how to achieve what seems simple enough.

       

      I declare two variables:

       

         [Bindable]
         private var dgIdx:int = new int();  //hold the dataGrid selectedIndex value
         [Bindable]
        private var vScroll:int = new int();  //hold the dataGrid verticalScroll position

       

      Make dataGrid:

       

      <mx:DataGrid id="dataGrid"

      dataProvider="{getSystems.lastResult}"

      itemClick="dataGrid_itemClickHandler(event)">

       

      Data grid item click handler:

       

      vScroll = dataGrid.verticalScrollPosition; //set scroll
      dgIdx = dataGrid.selectedIndex;  //index of clicked item

       

      Then I click an update button that writes any added information to the item clicked.

      In the result event of the update function I then have:

       

      getAllSystemsResult.token = systemService.getAllSystem();

      dataGrid.verticalScrollPosition = vScroll;
      dataGrid.selectedIndex = dgIdx;

       

      The data gets updated but the item isn't being selected or scrolled to in the datagrid after the dp update.

      The most frustrating part is I made a button with a click function containing the exact thing found in the Update Result event: 

       

      dataGrid.verticalScrollPosition = vScroll;
      dataGrid.selectedIndex = dgIdx;

       

      It then scrolls and selects perfectly.

      Why isn't this automation working for me in the update Result event but will on the button click?

       

      Thanks for any insight on what this noober is doing wrong.

      -aaron

        • 1. Re: datagrid set verticalScroll after data is updated - What does it take?
          Flex harUI Adobe Employee

          Try calling validateNow before setting the verticalScrollPosition

          1 person found this helpful
          • 2. Re: datagrid set verticalScroll after data is updated - What does it take?
            Aar0nm Level 1

            Thank you for the suggestion.

             

            I added this to the updateRecordResult(event:ResultEvent)

            (

            dataGrid.validateNow();
            dataGrid.verticalScrollPosition = vScroll;
            dataGrid.selectedIndex = dgIdx;

            )

             

            Even put in a break at the validateNow but it refuses to scroll or select until I click the button.

            • 3. Re: datagrid set verticalScroll after data is updated - What does it take?
              Flex harUI Adobe Employee

              What kind of dataprovider update is going on?   If it is a full replacement,

              when is it replaced?  Maybe it is after your updateRecordResult runs.

              • 4. Re: datagrid set verticalScroll after data is updated - What does it take?
                Aar0nm Level 1

                >What kind of dataprovider update is going on?

                 

                The dp update is:

                getAllSystemResult.token = systemService.getAllSystem();

                 

                That function is:

                public function getAllSystem() : mx.rpc.AsyncToken

                Which is a mysqli query via php returning an array of rows.

                 

                > If it is a full replacement,when is it replaced?

                Within the Update Result event. The data is updated based on a <status>true</status> xml return.
                Just one line above my attempts to validate and set verticalScroll.

                 

                I think this answers your query?

                 

                Really do appreciate your time.

                -aaron

                • 5. Re: datagrid set verticalScroll after data is updated - What does it take?
                  Flex harUI Adobe Employee

                  From the code snippets it isn't clear when the dp is actually replaced

                  relative to the code you are trying to run.  Make sure the order is correct

                  and validateNow is being called after the new dp is assigned.

                  • 6. Re: datagrid set verticalScroll after data is updated - What does it take?
                    Flex Rock Level 1

                    Hi,

                         try this

                    override

                     

                    protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void

                    {

                             dataGrid.validateNow();
                              dataGrid.verticalScrollPosition = vScroll;
                              dataGrid.selectedIndex = dgIdx;

                    }

                     

                     

                     

                     

                    Thanks

                    Jayagopal.

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                     

                    • 7. Re: datagrid set verticalScroll after data is updated - What does it take?
                      Flex Rock Level 1

                      Hi,

                          try this

                       

                         override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
                         {
                               dataGrid.validateNow();
                               dataGrid.verticalScrollPosition = vScroll;
                               dataGrid.selectedIndex = dgIdx;
                         }

                       

                       

                      Thanks,

                       

                      Jayagopal.

                      • 8. Re: datagrid set verticalScroll after data is updated - What does it take?
                        Aar0nm Level 1

                        The order seems correct.

                        The data is being updated as a result event of the service send (POST)

                        I'm not changing the DP just updating the result token.

                         

                        Here's the update record result event.

                         

                        protected function updateRecordResult(event:ResultEvent):void
                                  {    
                                   
                                        if (event.result.own == true)

                                       {
                                            Alert.show("Record successfully updated.", "Update Record");

                         

                                                getAllSystemResult2.token = systemService.getAllSystem();  \\ Here is dataset being 'refreshed'


                                         dataGrid.validateNow(); 
                                        dataGrid.verticalScrollPosition = vScroll;  \\now scroll
                                        dataGrid.selectedIndex = dgIdx;  \\now select
                                        }else{
                                          
                                            Alert.show("Update failed.", "Update Records");
                                        }       
                                    }

                         

                        Is that not the correct order?

                         

                        The override provided, once I put it in, I get an error on launch:

                        "Cannot access a property or method of a null object reference."


                                    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
                                    {
                                        dataGrid.validateNow();
                                        dataGrid.verticalScrollPosition = vScroll;
                                        dataGrid.selectedIndex = dgIdx;
                                    }

                         

                         

                        Since the button works, how can i call this button's function click?

                         

                        protected function button5_clickHandler(event:MouseEvent):void

                         

                        I tried just button5_clickHandler() but it wants a value, and I didn't know what to put in the parens...

                         

                        Thanks again for any help, Thank the gods it's Friday and being a n00b really bites.

                        -aaron

                        • 9. Re: datagrid set verticalScroll after data is updated - What does it take?
                          Flex Rock Level 1

                          Hi,

                              try this

                           

                          override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
                                      {

                                         super.updateDisplayList(unscaledWidth, unscaledHeight);

                           

                                          if(dgIdx!=null)

                                         {
                                               dataGrid.validateNow();
                                               dataGrid.verticalScrollPosition = vScroll;
                                               dataGrid.selectedIndex = dgIdx;
                                      }

                          }

                           

                           

                          Thanks

                          Jayagopal.

                          • 10. Re: datagrid set verticalScroll after data is updated - What does it take?
                            Aar0nm Level 1

                            Thank you for your suggestion Jayagopal.

                             

                            Error: Comparison between a value with static type int and a possibly unrelated type Null.

                             

                            I then tried if(dgIdx > 0) but when the application loaded all of the panels were on top of each other, so I couldn't even log in to get to the datagrid ('details' state)

                             

                            Respectfully,

                            Aaron

                            • 11. Re: datagrid set verticalScroll after data is updated - What does it take?
                              Aar0nm Level 1

                              I created an updateComplete function for the dataGrid and that seems to have worked.

                               

                              protected function dataGrid_updateCompleteHandler(event:FlexEvent):void

                              {

                              dataGrid.validateNow();
                              dataGrid.verticalScrollPosition = vScroll;
                              dataGrid.selectedIndex = dgIdx;

                              }

                               

                              I appreciate your time guys and I hope your weekend is a great one!

                              -aaron