YUI Library Home

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 point to the Yahoo! Video Search webservice without a proxy. The generateRequest() method must be customized to comply with the third-party 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.

Yahoo! Video Search:

Sample Code

Data:

1{"ResultSet"
2    {"totalResultsAvailable":"48"
3    "totalResultsReturned":10, 
4    "firstResultPosition":1, 
5    "Result": [ 
6        {"Title":"madonna_americanlife_9-rub08wm30p_450-v.asx"
7        "Summary":"Summary: Madonna- American life Noticias madonna - Noticias madonna - Noticias madonna -"
8        "Url":"http:\/\/www.warnerreprise.com\/asx\/madonna_americanlife_9-rub08wm30p_450-v.asx"
9        "ClickUrl":"http:\/\/www.warnerreprise.com\/asx\/madonna_americanlife_9-rub08wm30p_450-v.asx"
10        "RefererUrl":"http:\/\/www.descargaarchivos.com\/noticias\/index.php?query=madonna&type=video"
11        "FileSize":"30"
12        "FileFormat":"msmedia"
13        "Height":"264"
14        "Width":"352"
15        "Duration":"240"
16        "Streaming":"false"
17        "Channels":"2"
18        "Thumbnail":  
19            {"Url":"http:\/\/scd.mm-so.yimg.com\/image\/1702827152"
20            "Height":"108"
21            "Width":"145" 
22        } 
23    } 
24        ... 
25    ]} 
26
view plain | print | ?

CSS:

1#myAutoComplete { 
2    width:30em/* set width here or else widget will expand to fit its container */ 
3    padding-bottom:2em
4} 
5/* styles for custom formatting */ 
6.yui-ac .result {position:relative;height:62px;} 
7.yui-ac .name {position:absolute;bottom:0;left:64px;} 
8.yui-ac .img {position:absolute;top:0;left:0;width:58px;height:58px;border:1px solid black;background-color:black;color:white;} 
9.yui-ac .imgtext {position:absolute;width:58px;top:50%;text-align:center;} 
10.yui-ac img {width:60px;height:60px;margin-right:4px;} 
view plain | print | ?

Markup:

1<form action="http://video.search.yahoo.com/search/video" onsubmit="return YAHOO.example.CustomFormatting.validateForm();"
2    <h3>Yahoo! Video Search:</h3> 
3    <div id="myAutoComplete"
4        <input id="myInput" type="text" name="p"
5        <div id="myContainer"></div> 
6    </div> 
7</form> 
view plain | print | ?

JavaScript:

1YAHOO.example.CustomFormatting = (function(){ 
2    // Instantiate DataSource 
3    var oDS = new YAHOO.util.ScriptNodeDataSource("http://search.yahooapis.com/VideoSearchService/V1/videoSearch"); 
4    oDS.responseSchema = { 
5        resultsList: "ResultSet.Result"
6        fields: ["Title","Thumbnail"
7    }; 
8    // Setting to default value for demonstration purposes. 
9    // The webservice needs to support execution of a callback function. 
10    oDS.scriptCallbackParam = "callback"
11     
12    // Instantiate AutoComplete 
13    var oAC = new YAHOO.widget.AutoComplete("myInput","myContainer", oDS); 
14    // The webservice needs custom parameters 
15    oAC.generateRequest = function(sQuery) { 
16        return "?appid=YahooDemo&output=json&query=" + sQuery ; 
17    }; 
18    // Result data passed as object for easy access from custom formatter. 
19    oAC.resultTypeList = false
20    // Customize formatter to show thumbnail images 
21    oAC.formatResult = function(oResultData, sQuery, sResultMatch) { 
22        var img = "", nonimg = ""
23        var oThumbnail = oResultData.Thumbnail; 
24        if(oThumbnail && (oThumbnail !== "")) { 
25            img = "<img src=\""+ oThumbnail.Url + "\">"
26        } 
27        else { 
28            img = "<span class=\"img\"><span class=\"imgtext\">N/A</span></span>"
29        } 
30        return "<div class=\"result\">" + img + " <span class=\"name\">" + sResultMatch + "</span></div>"
31    }; 
32 
33    // Stub for form validation 
34    var validateForm = function() { 
35        // Validation code goes here 
36        return true
37    }; 
38     
39    return { 
40        oDS: oDS, 
41        oAC: oAC, 
42        validateForm: validateForm 
43    } 
44})(); 
view plain | print | ?

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.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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