1) Can someone explain in words what indexOf does? Obviously it
compares the two and spits out a number, but I just don't fully grasp
what is going on. Why use it instead of just == ?

- String.indexOf returns -1 if the specified sequence is not found in the target string (the one you are calling the method on) and returns the zero based index of the position within the target string if it is found. You would use indexOf if you were interested in substring (partial) matches. i.e. so your grid shows all titles with "ch" in them without requiring the title to BE "ch".

The problem you are having is that the filter calls methods on both title and docNumber without first checking if they are null. So, if either is null you are going to get exceptions.

I would probably re-write that as something like: Note... this code may not compile, but should be close :)

public function processFilter(item:Object):Boolean
{
var result:Boolean=false;


//make sure there is a search term
if(term.text != null && term.text.length > 0)
{ //get the search term as upper case
  var searchTerm:String = term.text.toUpperCase();

  //check against the title
  if(item.title != null && item.title.length > 0)
  {
     result = (item.title.toUpperCase().indexOf(searchTerm) != -1);
  }

  //no need to check doc number if title already matched or if there is no 
docNumber
  if(result == false && item.docNumber != null && item.docNumber.length > 0)
  {
     result = (item.docNumber.toUpperCase().indexOf(searchTerm) != -1);
  }
}

return result;
}

Though I may use RegExp and String.search() instead of indexOf...  Check out this 
page 
<http://livedocs.adobe.com/flex/3/html/help.html?content=09_Working_with_Strings_09.html>
 for more info on string searches.


hth
Scott

Scott Melby
Founder, Fast Lane Software LLC
http://www.fastlanesw.com
http://blog.fastlanesw.com



Don Kerr wrote:
I've been using this code below for a long time...but don't fully
understand how it works:) It does work great, as long as both title
and docNumber are not null. But it blows up if docNumber is null.

1) Can someone explain in words what indexOf does? Obviously it
compares the two and spits out a number, but I just don't fully grasp
what is going on. Why use it instead of just == ?

2) The OR || isn't clicking either. Would I add !item.docNumber.length
as another || to prevent the error when docNumber is blank? I want it
to filter on one or more columns in the dataProvider, but not blow up
if any one of them are blank. If one is blank, it should still filter
on the others.

public function processFilter(item:Object):Boolean
{
var result:Boolean=false;
// If no filter text, or a match, then true
if (!item.title.length
|| item.title.toUpperCase().indexOf(term.text.toUpperCase()) >= 0 ||item.docNumber.toUpperCase().indexOf(term.text.toUpperCase()) >= 0
)
result=true;
return result;
}

Thanks for the added insight into this borrowed code.:)
Don


Reply via email to