4 Replies Latest reply on Sep 13, 2006 9:58 AM by GeorgeWS

    Port AS2 datagrid filter to AS3, help

    Dr._Goomba Level 1
      I am trying to port an AS2 filter script to AS3. I was wondering if someone could help with this. Still very new to AS3 and only know the basics. Thanks!!!!


      [Bindable]
      public var filterBy:Array = ["movie_id","upc","title"];

      private function actionFilter():void {
      if(_global.arrMembers == undefined) _global.arrMembers = data.dataProvider.slice(0);
      var arrMembers = _global.arrMembers;
      var arrDisplay:Array = [];
      var fortext = forInput.text.toLowerCase();
      var selected = column.selectedItem.data;

      for(var i = 0; i < arrMembers.length; i++)
      {
      if(arrMembers [selected].substr(0,fortext.length).toLowerCase() == fortext)
      {
      arrDisplay.push(arrMembers
      );
      }
      }
      data.dataProvider = arrDisplay;
      }

      New data form elements:

      <mx:Label x="10" y="25" text="Filter by:"/>
      <mx:TextInput x="72" y="23" change="actionFilter()" id="forInput"/>
      <mx:Label x="240" y="25" text="in:"/>
      <mx:ComboBox x="268" y="23" id="column" dataProvider="{filterBy}" change="forInput.text=''"/>

      <mx:DataGrid dataProvider="{dvds}" name="data" x="10" y="51" width="669" height="218">
      <mx:columns>
      <mx:DataGridColumn headerText="" dataField="active" width="75"/>
      <mx:DataGridColumn headerText="Movie ID" dataField="movie_id" width="100"/>
      <mx:DataGridColumn headerText="Title" dataField="title"/>
      </mx:columns>
      </mx:DataGrid>
        • 1. Re: Port AS2 datagrid filter to AS3, help
          peterent Level 2
          You should use a Collection, such as ArrayCollection as the dataProvider to the DataGrid. Then you can use the Collection's filterFunction to hide and show values.

          [Bindable] public var dvds:ArrayCollection = new ArrayCollection();

          // place all of the data into this collection.

          function filterBy( item:Object ) : Boolean
          {
          // determine if the item is something you want to show and return true, otherwise return false
          }

          // associate the function with the Collection:

          dvds.filterFunction = filterBy;

          // now tell the Collection to refresh itself:
          dvds.refresh();

          Check the Flex 2 documentation on ICollectionView to see how it works.
          • 2. Re: Port AS2 datagrid filter to AS3, help
            GeorgeWS Level 1
            I have been using the exact same AS2 and would like to do the same thing (convert to AS3) I have been trying for a week I have the text filter part fine but cant get the combobox to change the column in the filterfunction. I really just wanted to join your topic in case there is an answer. Dr. we must be working on the same type of stuff. Thanks for both of your help, im barely eeking by.

            George
            • 3. Re: Port AS2 datagrid filter to AS3, help
              peterent Level 2
              The selection of an item from the ComboBox should change something that the filter function uses in its test, then trigger a refresh on the Collection:

              [Bindable] private var dp:ArrayCollection = new ArrayCollection( ...... );
              dp.filterFunction = filterBy;
              private var testValue:String;
              function filterBy( item ) : String {
              return item.field == testValue;
              }
              <mx:ComboBox ... change="testValue=event.target.selectedItem; dp.refresh()" ... />

              Anything which is bound to the dp as its dataProvider will see only values which pass the filter criteria.
              • 4. Re: Port AS2 datagrid filter to AS3, help
                GeorgeWS Level 1
                Alright now Im getting it. I got this to work. Thanks