2 Replies Latest reply on Aug 10, 2006 9:18 AM by kohinoor75

    DataGrid.placeSortArrow(). How to use it

      I want to have a dataGrid sorted upon dataProvider update, and I want the grid to display correctly the sort Arrow so that the user know what is the sorted column, and in what order.
      The placeSortArrow method seems to be a good candidate, and the documentation says : "Draws the sort arrow graphic on the column that is the current sort key"
      The question is : How to specify the sortColumn to the dataGrid ? I did not found how to do this.

        • 1. Re: DataGrid.placeSortArrow(). How to use it
          peterent Level 2
          The placeSortArrow() method is protected in the DataGrid class. This means you can only use it from a class which extends DataGrid, but 99.99% of the time you would not need to use it anyway.

          In Flex 2, you sort the data, not the DataGrid. The DataGrid has a user interface via a click on a column header that does it, but to programmatically sort the data - and thus make the sort arrow appear on a column - you must sort the DataGrid's dataProvider.

          Let's assume that dp is your dataProvider: dp:ArrayCollection and each item has 3 fields: col1, col2, and col3. To show the sort arrow on the second column ("col2") you need to do this:

          import mx.collections.SortField;
          import mx.collections.Sort;
          import mx.collections.ArrayCollection;

          // create an instance of the Sort class.
          var dataSort:Sort = new Sort();

          // set the sort fields in the Sort class to an Array of SortField instances. Here, there is a single item to sort "col2"
          // in ascending order. There are more arguments to the SortField constructor which includes descending sort.
          dataSort.fields = [ new SortField("col2") ];

          // Set the new Sort as the dataProvider's sort object:
          dp.sort = dataSort;

          // Whenever the Sort changes, you must refresh the dataProvider:

          When you do the above, the DataGrid will sort on col2 and the sort arrow will appear. One benefit of using the Sort object is that if you add anything to the dataProvider, it will be sorted automatically.

          If you plan on programmatically sorting the DataGrid more than just the initial time, put the Sort object in a member variable so you can change it easily. You might even consider making an Array of SortFields that you can reuse and just do dp.refresh() whenever you change any of them.
          • 2. Re: DataGrid.placeSortArrow(). How to use it
            kohinoor75 Level 1
            Thanks for this prompt and very detailed reply, it works fine (I did not succeed with that in Flex 1.5, so it's really cool)
            I should have more thoroughly studied the ArrayCollection interface...