This content has been marked as final. Show 2 replies
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:
// 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.
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...