4 Replies Latest reply on Sep 20, 2010 10:30 AM by mgk_3

    DataGrid resets on data change

    mgk_3

      Hello,

       

      I am using a DataGrid component to display some real-time statistics.  The dataprovider for my DataGrid is an XMLList.  I am not using HTTPService to get the XMLList; instead we have an in-house subscription service that provides me with a new XMLList every few seconds (representing a snapshot of an external database).

       

      The problem I am running into is that every time I receive a new XMLList and update the dataprovider for my DataGrid, the DataGrid resets its visual layout.  In particular the scrolling position and sorting properties get reset.  So for example, I can look at the DataGrid, sort on an arbitrary column and scroll halfway down the list.  Then in a few seconds, the DataGrid's dataprovider will refresh, and the DataGrid will snap back to the top of the list and will use the default sorting properties.

       

      From a usability perspective, the layout of the DataGrid wouldn't change when the dataprovider is refreshed, only the values.  But since I am given a new dataprovider every few seconds, I am not sure how I would achieve this functionality.  Any insight would be appreciated, and I am happy to provide more details if something didn't make sense.

       

      mgk3

        • 1. Re: DataGrid resets on data change
          Flex harUI Adobe Employee

          The mx:DataGrid relies on UIDs and CollectionEvents.  Elements in a

          collection are either assigned a UID or implement IUID and have a property

          that returns a UID.

           

          Of the many CollectionEventKinds, two of them, RESET and REFRESH, are used

          to notify the DataGrid about wholesale changes to the collection.  RESET

          says that the items have changes so significantly that any caching of UIDs,

          selection and scrollposition are void.  Setting a new dataProvider is

          effectively a RESET.  REFRESH implies that UIDs for items are still valid,

          but have been potentially given new indexes due to sorting or filtering.

           

          A third CollectionEventKind, UPDATE, indicates a change to properties on an

          item, but no change to UID or index.

           

          Every time you get a new XMLList, the nodes in the XMLList will have

          different UIDs because they are from a different XML tree and XML nodes do

          not implement IUID.

           

          I think your choices are:

          1) loop through the new XMLList, find the nodes in the old XMLListCollection

          and update them which should generate UPDATE events.

          2) Convert the XMLList to a custom ArrayCollection of ValueObjects that

          implement IUID, especially if there is a UID in the data like employee ID,

          serial #, record #, etc.  The custom ArrayCollection subclass would take the

          new Array of valueObjects and swap it in, suppressing the default RESET

          event and dispatching a REFRESH instead.

          3) save the scrollPosition and sort and re-apply them on every update.

          1 person found this helpful
          • 2. Re: DataGrid resets on data change
            mgk_3 Level 1

            This is great.  Your solutions make sense and seem like they would address my problem.  Thank you.

             

            I had something in mind along the lines of #1, so I will probably venture down that path for now.  I do have UIDs in my data, so #2 would probably work too, assuming that the REFRESH event doesn't reset the layout in the DataGrid component.  Do you know where I can look in the API to learn about how the DataGrid component handles these events?  I suppose I could also do some tests to figure this out, but I am pressed on time.

             

            Anyway, thanks again for the insight.  Exactly what I was looking for.

            • 3. Re: DataGrid resets on data change
              Flex harUI Adobe Employee

              The DataGrid behavior is probably not documented in the API.  When you sort

              the DG, a REFRESH event is sent, so that should give you an idea of what

              will happen.

              1 person found this helpful
              • 4. Re: DataGrid resets on data change
                mgk_3 Level 1

                Just wanted to say thanks again for the help on this.  I was able to get a working implementation without too much trouble.  I ended up implementing the IUID interface to my ValueObjects (second solution), since that ended up being a simpler approach than implementing a comparison algorithm.  Thanks!