moonrchand, (Also posted at http://jonathanbranam.net/solutions/filter-hierarchicalcollectionview-parent-child-datawith SWF and source code):
Any function applied to the filterFunction of HierarchicalCollectionView<http://jonathanbranam.net/flex3anatomy/class/HierarchicalCollectionView>is applied to all of the elements in the collection. It can filter items at different levels of the hierarchy as long as the method itself understands the hierarchy. You can view the example on a separate page<http://jonathanbranam.net/files/src/filter-hierarchy/FilterHierarchicalCollectionView.html>and view the source (View Source doesn't work in the embedded SWF below due to a bug in Flex Builder). This is the definition of the AdvancedDataGrid<http://jonathanbranam.net/flex3anatomy/class/AdvancedDataGrid>. Nothing special here: 1. <mx:AdvancedDataGrid id="adg" 2. dataProvider="{hd}" 3. width="100%" height="100%"> 4. <mx:columns> 5. <mx:AdvancedDataGridColumn 6. dataField="title"/> 7. </mx:columns> 8. </mx:AdvancedDataGrid> The filter function is applied like this: 1. // Set the filter function and refresh the collection view. 2. adg.hierarchicalCollectionView.filterFunction = myFilter; 3. adg.hierarchicalCollectionView.refresh(); And it uses the CheckBoxes defined here to enable and disable its operation: 1. <mx:CheckBox id="tParents" label="Parents that start with 'T'" change="criteriaChanged()"/> 2. <mx:CheckBox id="fourChildren" label="Parents with at least four children" change="criteriaChanged()"/> 3. <mx:CheckBox id="adChild" label="Children named 'A' or 'D'" change ="criteriaChanged()"/> Finally, here is the filter code. This would be simpler if you have a single filter you need to apply, but the example shows some dynamic behavior. The filter should return true for any item that is included in the display, and false otherwise. Notice that if you return false for a parent item, then it and all of its children will be filtered out. If you return true for a parent item, then you can still filter its children. 1. protected function myFilter(item:Object):Boolean 2. { 3. var title:String = item.title.toString(); 4. 5. // If the tParent filter is selected and this item it a parent 6. if (tParents.selected && item.parent) { 7. // Then fail if the name doesn't start with a "T". 8. if (title.substr(0,1).toUpperCase() != "T") { 9. return false; 10. } 11. } 12. 13. // If the fourParent filter is selected and this item is a parent 14. if (fourChildren.selected && item.parent) { 15. // Then fail if the parent doesn't have at least four children. 16. if (item.children.length < 4) { 17. return false; 18. } 19. } 20. 21. // if the adChild filter is selected and this is a child (not a parent) 22. if (adChild.selected && !item.parent) { 23. // Then fail if the child is not an "A" or a "D" 24. if (title.toUpperCase() != "A" && title.toUpperCase() != "D") { 25. return false; 26. } 27. } 28. 29. // Either all filters are disabled, or they all passed (or don't apply to this node) 30. return true; 31. } To filter a parent based on the data in its children, you simply have to process the children when the filter is asked about the parent. There must be some way for you to perform this operation on the parent. In this case, I have simply added a property to the parent object called parent and set it to true. There is nothing special about this property. You could also check the children property for null or check its length. In many cases, you will have a typed object that is the parent that is different than the type of the children. Explanation HierarchicalCollectionView<http://jonathanbranam.net/flex3anatomy/class/HierarchicalCollectionView>applies the filterFunction to the main data source and also to all of the open nodes in the hierarchy. When you apply a filter function and then refresh the list (don't forget the filter isn't applied until you call refresh()), it loops through all the open nodes and sets the filterFunction property and calls refresh() on each of them. Closed nodes are not processed until they are open. When the HierarchicalCollectionView<http://jonathanbranam.net/flex3anatomy/class/HierarchicalCollectionView>opens a node, it applies the filterFunction set on itself to the node. This will wipe out any filters you may have tried to set yourself, which is one reason why you can only have a single filter on the entire hierarchy. I have an example SWF on the site as well as the source code for the example. This is my first attempt to post the copy-paste HTML, so I hope the formatting comes out OK. http://jonathanbranam.net/solutions/filter-hierarchicalcollectionview-parent-child-data (Shorter equivalent link for mail client): http://jonathanbranam.net/node/48 http://jonathanbranam.net - Flex 3 Anatomy and Solutions From: moonrchand <[EMAIL PROTECTED]> > Date: Fri, Jun 20, 2008 at 9:42 AM > Subject: [flexcoders] How to filter HierarchicalCollectionView???? > To: [email protected] > > > Hi All, > How to filter HierarchicalCollectionView in order to display > parent branches with children matching the pattern, and filter out > parent branches without children matching the pattern ? > any help would be appreciated .. > > Thanks, > Chandra > > > > > > -- > http://jonathanbranam.net - Flex 3 Anatomy and Solutions

