YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: AutoComplete Control: Custom Formatting, with a Proxyless Remote DataSource

# AutoComplete Control: Custom Formatting, with a Proxyless Remote DataSource

This AutoComplete instance uses a ScriptNodeDataSource to to find images from the YQL webservice without a proxy. The generateRequest() method must be customized to comply with the open API. Please note that the ScriptNodeDataSource requires that the webservice support a callback mechanism.

A custom formatter function is defined in order to show thumbnail images in the results container.

View example in new window.

Yahoo! Image Search:

## Sample Code

Data:

{ "query": { "count": "10", "created": "2009-04-03T09:49:28Z", "lang": "en-US", "updated": "2009-04-03T09:49:28Z", "uri": "http://query.yahooapis.com/v1/yql?q=select+*+from+search.images+where+query%3D%22puppy%22+and+mimetype+like+%22%25jpeg%25%22", "diagnostics": { "publiclyCallable": "true", "url": { "execution-time": "47", "content": "http://boss.yahooapis.com/ysearch/images/v1/puppy?format=xml&start=0&count=10" }, "user-time": "50", "service-time": "47", "build-version": "911" }, "results": { "result": [ { "abstract": null, "clickurl": "http://lrd.yahooapis.com/_ylc=X3oDMTQ4djBydWlmBF9TAzIwMjMxNTI3MDIEYXBwaWQDb0pfTWdwbklrWW5CMWhTZnFUZEd5TkouTXNxZlNMQmkEY2xpZW50A2Jvc3MEc2VydmljZQNCT1NTBHNsawN0aXRsZQRzcmNwdmlkA0tPWDFWa2dlQXUxQ3I1Lm9zeG1zeWdUMDJKRTJEMG5XaEdnQUNGQUo-/SIG=12he0tu52/**http%3A//www.pc-wallpapers.co.uk/wallpapers/Animals/dogs/lonely_puppy_1024.jpg", "date": "2005/04/18", "filename": "lonely_puppy_1024.jpg", "format": "jpeg", "height": "768", "mimetype": "image/jpeg", "refererclickurl": "http://lrd.yahooapis.com/_ylc=X3oDMTQ4djBydWlmBF9TAzIwMjMxNTI3MDIEYXBwaWQDb0pfTWdwbklrWW5CMWhTZnFUZEd5TkouTXNxZlNMQmkEY2xpZW50A2Jvc3MEc2VydmljZQNCT1NTBHNsawN0aXRsZQRzcmNwdmlkA0tPWDFWa2dlQXUxQ3I1Lm9zeG1zeWdUMDJKRTJEMG5XaEdnQUNGQUo-/SIG=12c8c25vo/**http%3A//www.pc-wallpapers.co.uk/viewer.php%3Fcmax=4%26cat=DO%26id=000001", "refererurl": "http://www.pc-wallpapers.co.uk/viewer.php?cmax=4&cat=DO&id=000001", "size": "85300", "thumbnail_height": "120", "thumbnail_url": "http://thm-a01.yimg.com/image/21a5b2d2b9d3aa14", "thumbnail_width": "160", "title": "lonely_puppy_1024.jpg", "url": "http://www.pc-wallpapers.co.uk/wallpapers/Animals/dogs/lonely_puppy_1024.jpg", "width": "1024" } ... ] } } }

CSS:

label { color:#E76300; font-weight:bold; } #myAutoComplete { width:30em; /* set width here or else widget will expand to fit its container */ padding-bottom:2em; } /* styles for custom formatting */ .yui-ac .result {position:relative;height:62px;} .yui-ac .name {position:absolute;bottom:0;left:64px;} .yui-ac .img {position:absolute;top:0;left:0;width:58px;height:58px;border:1px solid black;background-color:black;color:white;} .yui-ac .imgtext {position:absolute;width:58px;top:50%;text-align:center;} .yui-ac img {width:60px;height:60px;margin-right:4px;}

Markup:

<label for="myInput">Yahoo! Image Search:</label> <div id="myAutoComplete"> <input id="myInput" type="text" name="p"> <div id="myContainer"></div> </div>

JavaScript:

YAHOO.example.CustomFormatting = (function(){ // Instantiate DataSource var oDS = new YAHOO.util.ScriptNodeDataSource("http://query.yahooapis.com/v1/public/yql?"); oDS.responseSchema = { resultsList: "query.results.result", fields: ["url", "thumbnail_url", "clickurl"] }; // Setting to default value for demonstration purposes. // The webservice needs to support execution of a callback function. oDS.scriptCallbackParam = "callback"; // Instantiate AutoComplete var oAC = new YAHOO.widget.AutoComplete("myInput","myContainer", oDS); // Bump up the query delay to reduce server load oAC.queryDelay = 1; // The webservice needs custom parameters oAC.generateRequest = function(sQuery) { return "q=select%20*%20from%20search.images%20where%20query%3D%22" + sQuery + "%22%20and%20mimetype%20like%20%22%25jpeg%25%22&format=json"; }; // Result data passed as object for easy access from custom formatter. oAC.resultTypeList = false; // Customize formatter to show thumbnail images oAC.formatResult = function(oResultData, sQuery, sResultMatch) { if(oResultData.thumbnail_url) { img = "<img src=\""+ oResultData.thumbnail_url + "\">"; } else { img = "<span class=\"img\"><span class=\"imgtext\">N/A</span></span>"; } return "<div class=\"result\">" + img + " <span class=\"name\">" + sResultMatch + "</span></div>"; }; oAC.itemSelectEvent.subscribe(function(sType, aArgs) { var oData = aArgs[2]; // object literal of selected item's result data // Redirect to the img window.location.href = oData.clickurl; }); // Stub for form validation var validateForm = function() { // Validation code goes here return true; }; return { oDS: oDS, oAC: oAC, validateForm: validateForm } })();

## 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:

- Basic Local Data
- Basic Remote Data
- Screenreader Accessibility
- Customizing Remote Requests
- Custom Formatting, with a Proxyless Remote DataSource
- Searching Field A, Submitting Field B with itemSelectEvent
- Combobox, with CSS for Multiple Stacked Instances
- Tagging Example with alwaysShowContainer
- FunctionDataSource to Search Multiple Fields of an Address Book at Runtime
- Centering AutoComplete On a Page
- Subset Matching
- Find Photos on Flickr
- Configurations Dashboard
- Skinning Model
- Flickr Image Search (included with examples for Rich Text Editor)
- Logger Integration with YUI Components (included with examples for Logger Control)
- Datatable with Autocomplete (included with examples for DataTable Control)
- Complex Application (included with examples for Layout Manager)

### More AutoComplete Control Resources:

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

Copyright © 2009 Yahoo! Inc. All rights reserved.

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