2 Replies Latest reply on Jun 13, 2012 11:00 AM by 56231

    How to sort the datagrid or dataprovider by number

    56231

      Some how my database got out of order (when viewed visually through phpmyadmin). Sometimes the rows are displayed in the phpmyadmin 3,1,2 instead of 1,2,3. Consequently when I retrieve the rows from the database to php and then to the flex datagrid, the rows are displayed 3,1,2 like the database. I think I may have reset the database and fixed it; However I would like to have a fail-safe way to insure the datagrid always displays the rows numerically in order by the image_no of each row retrieved from the database.

       

      I found this to learn from, but am having trouble making it work.

      http://www.flexafterdark.com/docs/Flex-CollectionSorting

       

      The code below has no effect on the order displayed in the grid. Can someone help me make this work. Thanks.

       

      import spark.collections.Sort;

      import spark.collections.SortField;

       

      private function getImagesResult(event:ResultEvent):void {
         images = event.result.image as ArrayCollection;

       

         var sortField2:SortField = new SortField("image_no");
         sortField2.numeric = true;
         sortField2.descending = true;
         images.sort = new Sort();
         images.sort.fields = [sortField2];
         images.refresh();
      }

       

      <mx:DataGrid dataProvider="{images.source}">
         <mx:columns>
            <mx:DataGridColumn dataField="image_no" />
            <mx:DataGridColumn dataField="image" />
         </mx:columns>
      </mx:DataGridColumn>