What is the difference between Children and dataProvider Elements in a Flex DataGrid?

Sean was having some trouble with the number of children in the dataProvider of a DataGrid. Here was his question:

I'm using a DataGrid component for a list of data. I have a timer that is set to auto-rotate through the elements in the DataGrid, one at a time every 5 seconds. However, the problem I am running into is that the numChildren property of the DataGrid does not appear to return the actual number of children in the list. It only appears to return the number of visible children in the list. Once it hits the visible bottom of the list, it goes back to the top... my question is:

Is there a property other than numChildren that will return the actual number of children in the DataGrid instead of only the visible number of children?

The numChildren property returns the number of children (AKA itemRenderer objects) in the DataGrid. Was was getting exactly what he was asking for. Unfortuantely, he was asking for the wrong thing. What he wanted was the number of elements in the dataProvider; which is a completely different number.

The DataGrid (and List, and probably other similar type components) only creates children (rows) based on what is shown on the screen. As you scroll through a DataGrid, the children (rows) are recycled. Instead of creating a new row for each element, it changes the content in an already existing one. This is done for performance reasons. If you have a list of 100 images, for example, but are only displaying 6 images you wouldn't want the Flash Player to render all 100 images.

To get the number of elements in the dataProvider, you need to go back to the dataProvider. If it's an ArrayCollection, you can use the length. Something like this would work DataGrid.dataProvider.length.

The difference between children and dataProvider elements was not intuitive to Sean. I guess if he really wanted to use the numChildren to get the value he wanted, he could extend the DataGrid and override the property. However, that would be inconsistent with how numChildren is handled in the rest of the framework. It refers to visual elements, not data elements.

Cookie Crisp for breakfast, I can't wait.

Comments
All Content Copyright 2005, 2006, 2007 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.8.