1 Reply Latest reply on Jun 18, 2007 2:01 PM by toofless

    Figuring out datagrid sort programmatically

    toofless Level 1
      OK I've got a datagrid with 4 columns. I'm giving the end user the opportunity to sort on any of these columns. They also have a report that is generated based on the data in the grid, but I'm wondering how determine on which column the grid is sorted, and which (ascending / descending) order so that I can sort the report in that exact same manner.

      I've added some code on the headerRelease event to track this information (storing it in the Cairngorm ModelLocator), but if you trace the results it doesn't foot to what you're actually doing, for example the following writes inconsistent (to the apparent action being taken in the interface) info to the console. I can always get the correct dataField property, but its sortDescending property is almost always opposite of what the datagrid displays:

      private function handleHeaderRelease(dgEvent:DataGridEvent):void
      {
      for(var k:int=0;k<myGrid.columns.length;k++)
      {
      trace(myGrid.columns[k].dataField + ":" + myGrid.columns[k].sortDescending);
      }
      trace("");
      }

      Does anyone know of a better way to figure out on which column the grid is currently sorted, and what sort nature is currently applied?

      TIA,
      Joe
        • 1. Re: Figuring out datagrid sort programmatically
          toofless Level 1
          OK I figured this out after remembering that the dataprovider of the datagrid is just an ArrayCollection - here's the answer for anyone who's looking for the same thing.

          In the function that calls the report:

          var s:Sort;
          var sF:SortField;
          s = summaryGrid.dataProvider.sort;
          // if sort is null, they haven't clicked any column headers, so use the default
          if(s == null)
          {
          urlVariables.sortOrder = "dtAdded";
          urlVariables.sortNature = "DESC";
          }
          /*
          otherwise use the first sort field
          (this could be modified to build an appropriate order by statement for multiple
          sort fields if necessary)
          */
          else
          {
          sF = s.fields[0];
          urlVariables.sortOrder = sF.name;
          urlVariables.sortNature = sF.descending?"DESC":"ASC";
          }

          Hope this helps someone!