3 Replies Latest reply on Dec 27, 2010 3:40 PM by saisri2k2

    Overriding dataProvider and filterFunction

    Zolotoj Level 3

      I am using a filterFunction for a dropdownlist. I am also overriding a  dataProvider for that control. Here is a code fragment:

      private var _dataProvider:IList;
      override public function set dataProvider(value:IList):void 
                {
                     //if IList is ICollection view, done
                     if(value is ICollectionView)
                     {
                          super.dataProvider = value;
                     }
                     
                     //make IList into ICollectionView
                     if(value is IList)
                     {
                          super.dataProvider = new ArrayCollection(value.toArray());
                     }
                     _dataProvider = value;               
                }
                override public function get dataProvider():IList
                {
                     return _dataProvider;
                }
      

      I have borrowed that code from the Internet.

       

      After I have added code for the override the filterFunction does not filter data out anymore. What I see is that the getter function returns the complete dataSource and that's why the control shows the full set data and not the filtered one.

       

      Any help?

        • 1. Re: Overriding dataProvider and filterFunction
          saisri2k2 Level 4

          try returning super.dataprovider?

          • 2. Re: Overriding dataProvider and filterFunction
            Zolotoj Level 3

            If I do it then I am getting an error

            Error #1034: Type Coercion failed: cannot convert mx.collections::ArrayCollection@b3daa01 to mx.collections.XMLListCollection

            at the following line:

            XMLListCollection(subdivisions.dataProvider).filterFunction = subdivisionsFilterFunction;

            where subdivisions is my dropdownlist.

            Here is its definition:

            package modulecode
            {     
                 
                 import flash.events.Event;
                 
                 import mx.collections.ArrayCollection;
                 import mx.collections.ICollectionView;
                 import mx.collections.IList;
                 import mx.collections.XMLListCollection;
                 import spark.components.DropDownList;
                 import spark.components.List;
            
                 [Bindble]
                 public class formDropDownList extends DropDownList
                 {
                      private var _dataToSelectedIndex:int;
                      private var _dataProvider:IList;
                      public function formDropDownList()
                      {
                           super();
                           addEventListener("change", eventHandler);
                      }
                      override public function set dataProvider(value:IList):void 
                      {
                           //if IList is ICollection view, done
                           if(value is ICollectionView)
                           {
                                super.dataProvider = value;
                           }
                           
                           //make IList into ICollectionView
                           if(value is IList)
                           {
                                super.dataProvider = new ArrayCollection(value.toArray());
                           }
                           _dataProvider = value;               
                           //super.dataProvider = value;
                           if (super.dataProvider != null)
                           {     
                                dataProvider.addEventListener("collectionChange", eventHandler);
                           }
                      }
                      override public function get dataProvider():IList
                      {
                           return super.dataProvider;
                           //return _dataProvider;
                      }
                      public function set dataToSelectedIndex(keyValue:int):void
                      {
                           if (selectedItem != undefined)
                           {     
                                return;
                           }     
                           if (keyValue == 0)
                           {     
                                selectedIndex = -1;
                           }     
                           else
                                for (var i:int=0;i<dataProvider.length;i++)
                                     if (Number(dataProvider[i]["id"].toString()) == keyValue)
                                     {     
                                          selectedIndex = i;
                                          break;
                                     }
                      }
                      [Bindable(event="Change")]
                      public function get dataToSelectedIndex():int
                      {
                           return _dataToSelectedIndex;
                      }
                                
                      private function eventHandler(event:Event):void
                      {                              
                           if (selectedItem != undefined)
                           {     
                                _dataToSelectedIndex = selectedItem.id;
                                // we cannot dispath parameter "event" because of 
                                // Error #2094: Event dispatch recursion overflow
                                dispatchEvent(new Event("Change"));
                           }
                           else
                                dataToSelectedIndex = 0;
                      }     
                 }
            }
            
            • 3. Re: Overriding dataProvider and filterFunction
              saisri2k2 Level 4

              I'm thinking you can just write (subdivisions.dataProvider).filterFunction  instead of casting it to XMLListCollection. and make sure that the data providers are bindable. including the setters and getters. see the followign Url.

               

              http://userflex.wordpress.com/2008/04/14/bindable-getters-and-setters/

               

              change: You need to do ArrayCollection(subdivisions.dataProvider).filterFunction  after reading the code again, I thought this will work.

               

              Message was edited by: saisri2k2