Ocelot20
Ocelot20

Reputation: 10800

HierarchicalCollectionView: One time sort?

I have an AdvancedDataGrid that relies on a HierarchicalCollectionView as it's dataProvider. What I'd like to do is sort the data when it is first loaded, but then disable the sort so that anything that is added after the initial load doesn't cause the grid to auto-sort again. I tried something like this:

this._myDataProvider = new HierarchicalCollectionView(
                           new HierarchicalData(this._model.rootTasks));

var mySort:Sort = new Sort();                   
mySort.fields = [new SortField("startDate")];

this._tasksDataProvider.sort = taskSorting;
this._tasksDataProvider.refresh();
this._tasksDataProvider.sort = null;

But setting the sort to null just leaves the data unsorted. I guess what I'm asking is: how can I sort the underlying hierarchical data since it seems setting the sort property will keep it dynamically sorting. Thanks for any help you can provide.

Upvotes: 1

Views: 798

Answers (3)

aavo
aavo

Reputation: 1

I had the same kind of problem until I realized that the sorting with Sort object does not change the "physical" ordering of the items within the Collection, so when you remove the Sort, the next refresh reverts the view to the actual "physical" ordering. Similarily as stated above, I solved by it by cloning the sub-collections into sorted order this way:

    public static function buildPositionSort():Sort
    {
        var dataSortField:SortField = new SortField();
        dataSortField.name = "position";
        dataSortField.numeric = true;
        dataSortField.descending = false;
        var sort:Sort = new Sort();
        sort.fields = [ dataSortField ];
        return sort;
    }
    /**
     * This method is used to create a clone of ArrayCollection, because sorting does not
     * actually change the physical ordering of the items.
     */
    public static function createSortedArrayCollectionCopy(source:ArrayCollection):ArrayCollection
    {
        var target:ArrayCollection = new ArrayCollection();
        source.sort = buildPositionSort();
        source.refresh();
        for each (var item:Object in source)
        {
            if (item.children != null) item.children = createSortedArrayCollectionCopy(item.children);
            target.addItem(item);
        }
        return target;
    }

Upvotes: 0

J_A_X
J_A_X

Reputation: 12847

Personally, I would change the sort order when you're getting the data. Either it's done on the server side or when you parse the data (ie. in your model). You can do a one time sort using Array with sortOn.

Upvotes: 2

Adrian Pirvulescu
Adrian Pirvulescu

Reputation: 4340

you can 1. sort the original data with sort function, 2. clone content and put it to a new collection with no sort (be careful do and make a manual clone), 3. just use the new data collection.

Upvotes: 0

Related Questions