Distinct Data Providers with GroupingCollection

Update: This post is updated and continued in the next post, “GroupingCollection -> HierarchicalCollectionView.” Please see that post for additional (better) suggestions.

This post is itself an update to “DistinctArrayCollection extends ArrayCollection.” The old post describes the problem and proposes a solution. I did not post code in that article because I felt like there had to be a better way of doing things. Here’s a better way (see the update for a much better way): use the GroupingCollection class in Flex 3. Continue reading to try it out and view code.

Try it out:

(Right click to view source)

Restating the problem

Example: Imagine a fictitious Shopping Cart application which displays the customer’s items in a DataGrid. Each item belongs to a category, such as clothing or tools or what have you. We would also like to display a list of the categories from which the customer has chosen products … that can get ugly. This is the elegant solution to that problem. And because we’re using GroupingCollection to create the grouped list of categories, the selected category in the ui component contains an ArrayCollection of the underlying items. This opens up all sorts of possibilities.

About the solution

The most compelling reason to use this solution is that it requires no additional data providers in your model just for the sake of displaying distinct records from a data set. This means clean separation between the data model and how that data is displayed in the view. Whenever the data provider in the model (in this case people) is updated, the bindings in the grouping collections update as well.

Oh, and this should get you started playing with the AdvancedDataGrid if you haven’t already. That’s a good thing too. Enjoy, and please comment!

Update: Please read “GroupingCollection -> HierarchicalCollectionView” for a(nother/ better) solution to this problem.

3 Responses to “Distinct Data Providers with GroupingCollection”


  1. 1 Matthew Wallace

    Dude this was totally what I was looking for. This totally fixes the problem I was having with separating complex data as I pull it in from say XML. The problem is how to keep data from repeating in lists that you don’t need say more than one position listed or whatever the case may be. So so glad that you found and posted them man. Thanks

    Matthew

  2. 2 T. Scot Clausing

    Glad to hear that this will help!

  3. 3 Irenegp

    thats it, bro

Leave a Reply