Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created February 27, 2010 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/316358 to your computer and use it in GitHub Desktop.
Save lsmith/316358 to your computer and use it in GitHub Desktop.
YAHOO.widget.AutoComplete.prototype.filterResults =
function( filter, response, parsedResponse, callback ) {
// If AC has passed a filter string value back to itself, grab it
if ( callback && callback.argument && callback.argument.query ) {
filter = callback.argument.filter;
}
filter = decodeURIComponent( filter ).toLowerCase();
// Only if a filter string is available to match against
if ( filter ) {
var results = parsedResponse.results,
filtered = [],
limit = callback.scope.maxResultsDisplayed,
fields = this.responseSchema.fields,
key = ( fields ? fields[0].key || fields[0] : null ),
lang = YAHOO.lang,
isString = lang.isString,
isArray = lang.isArray,
matched = 0,
i, len, result;
for( i = 0, len = results.length; i < len; ++i ) {
result = results[i];
if ( !isString( result ) ) {
if ( isArray( result ) ) {
result = result[0];
} else if ( key ) {
result = result[ key ];
} else if ( this.key ) {
result = result[ this.key ];
} else {
result = '';
}
}
if ( result.toLowerCase().indexOf( filter ) > -1 ) {
matched = filtered.push( results[i] );
}
// Filter no more if maxResultsDisplayed is reached
if ( matched >= limit ) {
break;
}
}
parsedResponse.results = filtered;
}
return parsedResponse;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment