6 Replies Latest reply on Mar 1, 2010 1:55 AM by TBSALLING

    How to keep DataGrid sort after data update?

    TBSALLING

      I use a datagrid where the user can click on any column header to have the rows sorted.

       

      The datagrid is filled with data from an XML datasource (e4x).

       

      Every time I add or remove a row form the table (by updating the data source through e4x expressions), the table sort is lost. The small sort-arrow in the coloumn header disappears, and the table contents are updated to show the rows un-sorted.

       

      How can I keep the datagrid sort when adding or removing data from the dataprovider?

       

      Thanks in advance,

      /Thomas.

        • 1. Re: How to keep DataGrid sort after data update?
          Flex harUI Adobe Employee

          You can save away the dataProvider.sort and restore it.  Seems inefficient

          to be adding and removing by replacing the whole source though.

          • 2. Re: How to keep DataGrid sort after data update?
            TBSALLING Level 1

            Hi,

             

            Thank you for the reply. I have been trying to make it work; but I am still not quite there. In more detail, I have an mxml file which these contents (fragment):

             

                [Bindable]

                 private var employees:XMLList = new XMLList();

                <mx:DataGrid id="dg" dataProvider="{employees}">

                     <mx:columns>

                        <mx:DataGridColumn headerText="Hire date" dataField="hireDate" />
                        <mx:DataGridColumn headerText="Fire date" dataField="fireDate" />
                        <mx:DataGridColumn headerText="Full name" dataField="fullName" />
                        ...
                    </mx:columns>
                </mx:DataGrid>

             

            Then I have an event handler, which is called (from BlazeDS) when a new employee is created. It looks like this:

             

               private function handleEmployeeBlazeCreated(event:XMLEvent): void

            {

                   // Up till here, the data grid is perfectly sorted by column of user choice

               employees += event.xml;             

              // Now the datagrid has lost its sort, and rows are shown in random order

            }

             

            Can you spell out to me, what the handleEmployeeBlazeCreated(...) method should do in order to keep the datagrid sorted in accordance with the column previously clicked by the user?

             

            Thanks in advance,

            /Thomas.

            • 3. Re: How to keep DataGrid sort after data update?
              Flex harUI Adobe Employee

              Var oldSort:Sort = dg.dataProvider.sort as Sort;

              Employees += event.xml;

              Dg.dataProvider.sort = oldSort;

              1 person found this helpful
              • 4. Re: How to keep DataGrid sort after data update?
                TBSALLING Level 1

                Sorry, but even with the code you propose, the datagrid is left in an unsorted state, after the employee has been added.

                • 5. Re: How to keep DataGrid sort after data update?
                  Yennick_Trevels Level 2

                  Wrap the employees XMLList in a XMLListCollection like this:

                  var employeesCollection:XMLListCollection  = new XMLListCollection(employees);

                   

                  Then pass the employeesCollection to the dataprovider instead of the employees XMLList.

                  Now when you reassign the sort, call employeesCollection.refresh() so that the sort result is shown.

                  1 person found this helpful
                  • 6. Re: How to keep DataGrid sort after data update?
                    TBSALLING Level 1

                    Thanks alot guys. The combination of your two replies made the sum. I needed to add the refresh(); but it seems to work without going through the Collection interface.

                     

                    So - this handler behaves correctly:

                     

                            private function handleEmployeeBlazeCreated(event:XMLEvent): void
                            {
                                var oldSort:Sort = dg.dataProvider.sort as Sort;
                                employees += event.xml;
                                dg.dataProvider.sort = oldSort;   
                                dg.dataProvider.refresh();       
                            }

                     

                    Thanks to both of you!

                     

                    BR

                    /Thomas.