0 Replies Latest reply on May 4, 2010 4:26 AM by EricJ32

    Very straightforward filtering question

    EricJ32

      OK, I'm after a "best practice" for this...

       

      I have an ArrayCollection of people, which is bound to a datagrid. There is a search facility on the datagrid - a TextInput which, when changed, uses a filterFunction on the ArrayCollection to see if the Name starts with that entered string.

       

      Easy Peasy.

       

                  protected function txtSearch_changeHandler(event:Event):void
                  {
                      dg.dataProvider.filterFunction = nameFilter;
                      dg.dataProvider.refresh();
                  }
                 
                  private function nameFilter( itm : Object ) : Boolean
                  {
                      return itm.Name.toLowerCase().indexOf(txtSearch.text.toLowerCase()) == 0;
                  }

       

          <mx:HBox>
              <mx:Label
                  text="Search:"/>
              <mx:TextInput
                  id="txtSearch"
                  change="txtSearch_changeHandler(event)"/>
          </mx:HBox>
         
          <mx:DataGrid
              id="dg"
              dataProvider="{model.people}"
              height="100%">
              <mx:columns>
                 
                  <mx:DataGridColumn
                      headerText="Name"
                      dataField="Name" />
                 
              </mx:columns>
          </mx:DataGrid>

       

      However, I don't want the model.people ArrayCollection to be filtered elsewhere in my application. So, what is the best practice for doing this?

       

      I was thinking, do I have a copy of the ArrayCollection in this component, which I then use BindingUtils.bindProperty to make sure it stays in sync with the model.people collection? Or is there a better solution?

       

      Thanks!