YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: AutoComplete Control: Find Photos on Flickr

# AutoComplete Control: Find Photos on Flickr

This example uses AutoComplete to find images by tag from the Flickr webservice. A simple PHP proxy is used to access the remote server via XHR. The `generateRequest()` method has been customized in order send additional required parameters to the Flickr application. The `formatResult()` method has been customized in order to display images in the results container, and the default CSS has been enhanced so the results container can scroll. Finally, a `itemSelectEvent` handler has been defined to collect selected images in a separate container.

View example in new window.

Find photos by tag and collect your selections:

##### Selections

## Sample Code

CSS:

#flickrselections { float:right; width:240px; padding:10px; background-color:#FFA928; } #flickrselections h5 { color:#009; margin:0; } /* custom styles for scrolling container */ #flickrautocomplete { width:15em; /* set width of widget here */ padding-bottom:2em; } #flickrautocomplete .yui-ac-content { max-height:30em;overflow:auto;overflow-x:hidden; /* set scrolling */ _height:30em; /* ie6 */ } #flickrautocomplete .flickrImg { width:6em;height:6em;padding:.1em;vertical-align:middle; }

Markup:

<label for="flickrinput">Find photos by tag and collect your selections:</label> <div id="flickrselections"> <h5>Selections</h5> <div id="photos"></div> </div> <div id="flickrautocomplete"> <input id="flickrinput" type="text"> <div id="flickrcontainer"></div> </div>

JavaScript:

YAHOO.example.ACFlickr = function() { // Set up a local proxy to YQL var myDS = new YAHOO.util.XHRDataSource("assets/php/yql_proxy.php?format=xml&q=select%20*%20from%20flickr.photos.search"); myDS.responseSchema = { resultNode: "photo", fields: ["title", "id", "owner", "secret", "server"] }; myDS.responseType = YAHOO.util.XHRDataSource.TYPE_XML; myDS.maxCacheEntries = 100; // Instantiate AutoComplete var myAC = new YAHOO.widget.AutoComplete("flickrinput","flickrcontainer", myDS); myAC.resultTypeList = false; myAC.suppressInputUpdate = true; myAC.generateRequest = function(sQuery) { return "%20where%20tags%3D%22"+sQuery+"%22"; }; // Helper function to generate the img tag var getImgMarkup = function(oPhoto, sSize) { var sUrl = "http://static.flickr.com/" + oPhoto.server + "/" + oPhoto.id + "_" + oPhoto.secret + "_"+ (sSize || "s") +".jpg"; return '<img src="' + sUrl + '" class="flickrImg" alt="' + oPhoto.title + '">'; }; // Helper function to generate the link tag var getImgLink = function(oPhoto) { return '<a href="http://www.flickr.com/photos/' + oPhoto.owner + "/" + oPhoto.id + '">'; }; // Custom formatting to display photos in the suggestions container myAC.formatResult = function(oResultItem, sQuery) { // This was defined by the schema array of the data source var sTitle = oResultItem.title, sMarkup = getImgMarkup(oResultItem) + " " + sTitle; return (sMarkup); }; // Selecting a photo adds it to the selections area myAC.itemSelectEvent.subscribe(function(sType, aArgs){ var oPhoto = aArgs[2]; YAHOO.util.Dom.get("photos").innerHTML = '<p>' + getImgLink(oPhoto) + getImgMarkup(oPhoto, "m")+'</a></p>'+ YAHOO.util.Dom.get("photos").innerHTML; }); return { oDS: myDS, oAC: myAC }; }();

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### AutoComplete Control Examples:

### More AutoComplete Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings