6 Replies Latest reply on Apr 17, 2009 1:43 PM by Kurrykid

    What's the Best Way to Search a Datagrid?

    Kurrykid Adobe Community Professional

      I want to setup search functionality for a datagrid.  I have done a lot of research but have only been able to come up with a single column solution.  Can anyone give some recommendations on how I can search all the data that is in the datagrid?

       

      Thanks.

       

      Dave

        • 1. Re: What's the Best Way to Search a Datagrid?
          atta707 Level 2

          DataGrid's dataProvider is always an ICollectionView instance: ArrayCollection or XMLListColection.

           

          ICollectionView has a filterFunction that would reference to a function with a particualr signature:

           

          private function filter(item:Object) : Boolean {


          }

           

          when you have assinged this function to the array collection and do a refresh() on it this function would be called for each element in the underlying dataProvider. The 'item' is current object, the row if you will, in your dataProvider for which this method is being called. Now that you have the complete object availale you have all it's properties available; do whatever checks you need and return true if you want to keep the row in the filtered list of false otherwise. That's it!

           

          Flex Builder help has many examples on filterFunction as does google!

           

          Good luck!

           

          ATTA

          • 2. Re: What's the Best Way to Search a Datagrid?
            Kurrykid Adobe Community Professional

            That works good...one more question though.  It appears the filterFunction is case sensitive.  Is there anyway to tell it to find "ball" or "Ball" with the same filter?

             

            Thanks.

             

            Dave

            • 3. Re: What's the Best Way to Search a Datagrid?
              atta707 Level 2

              http://livedocs.adobe.com/flex/3/langref/String.html#toLocaleLowerCase()

               

              As you noticed, the logic of qualifying for this search is in your hands! ICollectionView simply called your function to decide this. Now, once you've a search string a another string which you're looking for a match, you'd want to convert both of these to lower/upper case by using some method like: http://livedocs.adobe.com/flex/3/langref/String.html#toLocaleLowerCase(). Once the case of both string has been changed, now do a comparison.

               

              ATTA

              1 person found this helpful
              • 4. Re: What's the Best Way to Search a Datagrid?
                Kurrykid Adobe Community Professional

                Thanks Atta...you have clearly answered my question but before I close it out, I have one more thing I'd like to accomplish.  How can I get it to search partial responses so I can pull keywords from the text?  For example, if one of the fileds has "The ball went over the hill".  If I do a search for ball, it won't return as an item.  Is there anyway to use a wildcard in the search so that typing "ball" would return the desired results?

                 

                Thanks agqain.

                 

                Dave

                • 5. Re: What's the Best Way to Search a Datagrid?
                  EvolvedDSM Level 2

                  To accomplish searching any part of your object, try this.  This is from my own code, but I've tried to explain each part:

                   

                  I have a text input named alertTextFilter and this is where you can type in your search criteria to filter the datagrid.

                   

                  private function filterAlerts():void{
                          ac.filterFunction = appFilter;
                          ac.refresh();

                  }
                      // Filters DG
                      private function appFilter(obj:Object):Boolean{
                          var checkForMatches:Boolean = false
                          if(obj.clientName.toLowerCase().search(alertTextFilter.text.toLowerCase()) != -1){
                              isMatch = true;
                          }             
                          return checkForMatches;
                      }

                   

                  obj is your arrayCollection object.  clientName is the property in my arrayCollection that I want to search.  The .search() is a handy function that looks within the value specified in the ().  alertTextFilter.text is the value of search text.  So... my search text input is named alertTextFilter, and the value of this text input is alertTextFilter.text.   Lastly, .toLowerCase() ensures that everything will follow the same case.  = -1 is the boolean check...

                   

                  Now to get it to filter on the fly, in your text input, set the property change="filterAlerts()"

                   

                   

                  Hope this helps.

                  1 person found this helpful
                  • 6. Re: What's the Best Way to Search a Datagrid?
                    Kurrykid Adobe Community Professional

                    That's great...thanks so much.