yunshichen

我相信人生是值得活的,尽管人在一生中必须遭受痛苦,卑劣,残酷,不幸和死亡的折磨,我依然深信如此.但我认为人生不一定要有意义,只是对一些人而言,他们可以使人生有意义. ---J 赫胥黎

YUI :一个autocomplete 组件的例子


< html >
<!-- CSS file (default YUI Sam Skin)  -->
< link  type ="text/css"  rel ="stylesheet"  href ="http://yui.yahooapis.com/2.5.2/build/autocomplete/assets/skins/sam/autocomplete-skin.css" >

<!--  Dependencies  -->
< script  type ="text/javascript"  src ="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js" ></ script >

<!--  OPTIONAL: Connection (required only if using XHR DataSource)  -->
< script  type ="text/javascript"  src ="http://yui.yahooapis.com/2.5.2/build/connection/connection-debug.js" ></ script >

<!--  OPTIONAL: JSON (enables JSON validation)  -->
< script  type ="text/javascript"  src ="http://yui.yahooapis.com/2.5.2/build/json/json-debug.js" ></ script >
<!--  Source file  -->
< script  type ="text/javascript"  src ="http://yui.yahooapis.com/2.5.2/build/autocomplete/autocomplete-debug.js" ></ script >

<!-- ================== Logger ====================  -->
< link  type ="text/css"  rel ="stylesheet"  href ="http://yui.yahooapis.com/2.5.2/build/logger/assets/skins/sam/logger.css" >
< script  type ="text/javascript"  src ="http://yui.yahooapis.com/2.5.2/build/logger/logger-debug.js" ></ script >

< style  type ="text/css" >
#myAutoComplete 
{
    width
: 15em ;   /*  set width here  */
    padding-bottom
: 2em ;
}
</ style >
< body  class =" yui-skin-sam" >
< div  id ="myLogger" ></ div >< br >
< hr >
Change datasource :
&nbsp;&nbsp;&nbsp; < input  type ="button"  id ="jsBtn"  value ="JS array"   />
&nbsp;&nbsp;&nbsp; < input  type ="button"  id ="funcBtn"  value ="JS function"   />
< br >< br >
< div  id ="myAutoComplete" >
    
< input  id ="myInput"  type ="text" >
    
< div  id ="myContainer" ></ div >
</ div >
< script  type ="text/javascript" >

// A JS array data source
var  testData = [ " fa " " fb " " fc " ];
//  A JavaScript Function DataSource
function  myFunction(sQuery) {
    aResults 
=  [];
    
if  (sQuery  &&  sQuery.length  >   0 ) {
        
var  charKey  =  sQuery.substring( 0 1 ).toLowerCase();
        
var  oResponse  =  dataset[charKey];
        
if  (oResponse) {
            
for  ( var  i  =  oResponse.length  -   1 ; i  >=   0 ; i -- ) {
                
var  sKey  =  oResponse[i].STATE;
                
var  sKeyIndex  =  encodeURI(sKey.toLowerCase()).indexOf(sQuery.toLowerCase());
                
if  (sKeyIndex  ===   0 ) {
                    aResults.unshift([sKey, oResponse[i].ABBR]);
                }
            }
            
return  aResults;
        }
    } 
else  {
        
for  ( var  letter  in  dataset) {
            
var  oResponse  =  dataset[letter];
            
for ( var  i  =   0 ; i  <  oResponse.length; i ++ ) {
                aResults.push([oResponse[i].STATE, oResponse[i].ABBR]);
            }
        }
        
return  aResults;
    }
}

var  dataset  =  {
    'a' : [{
" STATE "  :  " Alabama " " ABBR "  :  " AL " },
        {
" STATE "  :  " Alaska " " ABBR "  :  " AK " },
        {
" STATE "  :  " Arizona " " ABBR "  :  " AZ " },
        {
" STATE "  :  " Arkansas " " ABBR "  :  " AR " }],
    'b' : [ ],
    'c' : [{
" STATE "  :  " California " " ABBR "  :  " CA " },
        {
" STATE "  :  " Colorado " " ABBR "  :  " CO " },
        {
" STATE "  :  " Connecticut " " ABBR "  :  " CT " }],
     
//  Entire dataset not shown
};


// Init autocomplete component.
var  g_oAuto = new   function (){    
    
// ============ init logger
     this .logger  =   new  YAHOO.widget.LogReader( " myLogger " ); 
    
this .myDatasource  =   new  YAHOO.widget.DS_JSFunction(myFunction);
    
this .myAutoComp = new  YAHOO.widget.AutoComplete( " myInput " , " myContainer " this .myDatasource);
};

// Util functions.
function  changeDSToJsArray(){    
    g_oAuto.myAutoComp.dataSource
=   new  YAHOO.widget.DS_JSArray(testData);
    YAHOO.log(
" Changed data source to JS array " , " info " , this .toString());
};
function  changeDSToJsFunction(){
    g_oAuto.myAutoComp.dataSource
=   new  YAHOO.widget.DS_JSFunction(myFunction);
    
// Customize result view.
    g_oAuto.myAutoComp.formatResult  =   function (oResultItem, sQuery) {
        
var  sMarkup  =  oResultItem[ 0 +   "  ( "   +  oResultItem[ 1 +   " ) " ;
        
return  (sMarkup);
    };

    YAHOO.log(
" Changed data source to JS function " , " info " , this .toString());
};

// Set button event handlers.
var  btn  =  YAHOO.util.Dom.get( " jsBtn " );
btn.onclick
= changeDSToJsArray;

btn 
=  YAHOO.util.Dom.get( " funcBtn " );
btn.onclick
= changeDSToJsFunction;


</ script >

</ body >
</ html >

posted on 2008-09-12 15:42 Chenyunshi 阅读(1398) 评论(0)  编辑 收藏 引用 所属分类: Web development

只有注册用户登录后才能发表评论。
<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(7)

随笔分类

随笔档案

文章分类

相册

搜索

最新评论

阅读排行榜

评论排行榜