DWR实现搜索自动提示

 实现了一个类似google suggest的功能,拿来和大家分享一下。主要使用javascipt + Dwr + spring +hibernate
使用Dwr当然是免不了要配置,先来看一下Dwr是如何把我们的service层(spring bean中的)方法 暴露给前端js的,它使得前端使用service就像使用javascript自定义的函数一样方便。
xml 代码
xml 代码
  1. <dwr>  
  2.     <allow>  
  3.         <!-- DistrictLocatin -->  
  4.         <create creator="spring" javascript="DistrictLocationService">  
  5.             <param name="beanName" value="districtLocationService" />  
  6.             <!--<param name="location" value="./spring-action.xml" />-->  
  7.             <include method="findAllByLocationRangeFor51ditu" />  
  8.             <include method="findAllByKeywords" />  
  9.         create>  
  10. <convert converter="bean" match="com.goojia.dal.model.DistrictLocation">  
  11.             <param name="include" value="id,districtName,avgPrice,address,houseCount,lng51,lat51">param>  
  12.         convert>  
  13. allow>  
  14. dwr>  

 

java 代码
  1. service 层查找方法   
  2.   
  3.   
  4. /**  
  5.      * 根据输入的关键字查询小区信息,最大返回MAX_DISTRICT条记录  
  6.      * @param keywords  
  7.      * @return  
  8.      */  
  9.     public List  findAllByKeywords(String keywords){   
  10.  }  

 前面使用非常简单。只需要把生成的javascript导入 把componet组件放到你想放文本输入框的地方,并设置一下你想使用的样式,如果不传,将使用默认的样式  

<script src="<ww:url value="/dwr/interface/DistrictLocationService.js" includeParams="none" />" type="text/javascript"></script>
<script src="<ww:url value="/dwr/engine.js" includeParams="none" />" type="text/javascript"></script>
<script src="<ww:property value=" type="text/javascript" picurl></script>         
<script src="<ww:property value=" type="text/javascript" picurl></script>

html 代码
  1. <ww:component  id="district" name="districtName" template="autocomplete">  
  2.       <ww:param name="size">40ww:param>  
  3.       <ww:param name="maxlength">30ww:param>  
  4.       <ww:param name="dwrMethod">DistrictLocationService.findAllByKeywordsww:param>  
  5.       <ww:param name="hiddenId">'districtId'ww:param>  
  6.     ww:component>        
js 代码
  1. TextSuggest = Class.create();   
  2.   
  3. TextSuggest.prototype = {   
  4.     //构造函数   
  5.    initialize: function(elementId /*:HTMLInputElementId*/,oProvider /*:SuggestionProvider*/, options/*:cssStyle and display count  ect*/) {   
  6.       this.id          = elementId;        
  7.       this.provider    = oProvider;   
  8.       var browser      = navigator.userAgent.toLowerCase();   
  9.       this.isIE        = browser.indexOf("msie") != -1;   
  10.       this.isOpera     = browser.indexOf("opera")!= -1;   
  11.       this.isMozilla   = (browser.indexOf("gecko")!= -1 && browser.indexOf("mozilla")!= -1);   
  12.       this.textInput   = $(this.id);          
  13.       this.suggestions = new Array();   
  14.       this.setOptions(options);   
  15.       this.injectSuggestBehavior();   
  16.    },   
  17.     //设置参数   
  18.    setOptions: function(options) {   
  19.       this.options = {   
  20.          //层样式   
  21.          suggestDivClassName: 'suggestDiv',   
  22.          //选项样式   
  23.          suggestionClassName: 'suggestion',   
  24.          //匹配样式   
  25.          matchClassName     : 'match',   
  26.          //是否匹配输入框宽度   
  27.          matchTextWidth     : true,   
  28.          //选项颜色   
  29.          selectionColor     : '#b1c09c',   
  30.           //是否从头匹配   
  31.          matchAnywhere      : false,   
  32.          //是否忽略大小写   
  33.          ignoreCase         : false,   
  34.          //显示数目   
  35.          count              : 10,   
  36.           //隐藏字段的Id    
  37.          hiddenId           : ''                 
  38.       }.extend(options || {});   
  39.    },   
  40.     //注入输入提示行为   
  41.    injectSuggestBehavior: function() {   
  42.              
  43.       if ( this.isIE ||this.isMozilla){             
  44.           this.textInput.setAttribute('autocomplete','off');             
  45.       }   
  46.             
  47.     //创建控制器   
  48.       var keyEventHandler = new TextSuggestKeyHandler(this);   
  49.       //主要是为了避免在按回车的时候把表单提交 目前未能实现   
  50.       //new Insertion.After( this.textInput,'' );   
  51.      // new Insertion.After( this.textInput,'' );       
  52.       //创建div层   
  53.       this.createSuggestionsDiv();   
  54.    },      
  55.     //处理输入信息   
  56.    handleTextInput: function() {        
  57.      var previousRequest  = this.lastRequestString;        
  58.      this.lastRequestString =this.textInput.value.replace(/(^/s*)|(/s*$)/g, "");          
  59.      if ( this.lastRequestString == "" ||this.lastRequestString.length <=0 ){   
  60.           //如果有hidden的变量,要将变量置为空   
  61.          if(this.options.hiddenId != "" && this.options.hiddenId.length >0){               
  62.             $(this.options.hiddenId).value = "";                
  63.            }                   
  64.          this.hideSuggestions();    
  65.      }else if ( this.lastRequestString != previousRequest ){       
  66.           //访问数据源               
  67.         this.sendRequestForSuggestions();    
  68.      }            
  69.    },   
  70.     //选择框上移   
  71.    moveSelectionUp: function() {   
  72.       if ( this.selectedIndex > 0 ) {   
  73.          this.updateSelection(this.selectedIndex - 1);   
  74.       }   
  75.    },   
  76.     //选择框下移   
  77.    moveSelectionDown: function() {   
  78.       if ( this.selectedIndex < (this.suggestions.length - 1)  ) {   
  79.          this.updateSelection(this.selectedIndex + 1);   
  80.       }   
  81.    },   
  82.     //更新当前选择信息   
  83.    updateSelection: function(n) {   
  84.       var span = $( this.id + "_" + this.selectedIndex );   
  85.       if ( span ){   
  86.           //消除以前的样式   
  87.          span.style.backgroundColor = "";   
  88.       }   
  89.       this.selectedIndex = n;   
  90.       var span = $( this.id + "_" + this.selectedIndex );   
  91.       if ( span ){   
  92.           //更新新样式   
  93.          span.style.backgroundColor = this.options.selectionColor;   
  94.       }   
  95.    },   
  96.     //发送请求   
  97.    sendRequestForSuggestions: function() {   
  98.      if ( this.handlingRequest ) {   
  99.         this.pendingRequest = true;   
  100.         return;   
  101.      }   
  102.   
  103.      this.handlingRequest = true;   
  104.      this.callDWRAjaxEngine();   
  105.    },   
  106.   
  107.     //使用DWR访问后台   
  108.    callDWRAjaxEngine: function() {   
  109.            //清空以前的记录   
  110.            if(this.suggestions.length>0){   
  111.              this.suggestions = null;   
  112.            }             
  113.            //保存当前对象指针   
  114.            var tempThis = this;              
  115.            this.provider(this.lastRequestString,function(ajaxResponse){              
  116.             tempThis.suggestions = ajaxResponse;               
  117.               if ( tempThis.suggestions.length == 0 ) {   
  118.                   //如果有hidden的变量,要将变量置为空   
  119.                  if(tempThis.options.hiddenId != "" && tempThis.options.hiddenId.length >0){   
  120.                      $(tempThis.options.hiddenId).value = "";   
  121.                  }                   
  122.                  tempThis.hideSuggestions();                    
  123.                    
  124.               }else {                   
  125.                  tempThis.updateSuggestionsDiv();   
  126.                  tempThis.showSuggestions();   
  127.                  tempThis.updateSelection(0);                    
  128.               }   
  129.               tempThis.handlingRequest = false;   
  130.             if ( tempThis.pendingRequest ) {                     
  131.                  tempThis.pendingRequest    = false;                                                  
  132.                  tempThis.lastRequestString = this.textInput.value;                                  
  133.                  tempThis.sendRequestForSuggestions();   
  134.               }   
  135.            });   
  136.    },   
  137.    //显示信息   
  138.    setInputFromSelection: function() {        
  139.      var suggestion  = this.suggestions[ this.selectedIndex ];          
  140.      if(typeof(suggestion) != 'undefined'){   
  141.            //如果有hidden的变量,要将变量置赋值   
  142.           if(this.options.hiddenId != "" && this.options.hiddenId.length >0){          
  143.             $(this.options.hiddenId).value = suggestion.id;               
  144.            }            
  145.           this.textInput.value = suggestion.districtName;   
  146.                 
  147.        }        
  148.      this.hideSuggestions();        
  149.    },   
  150.       
  151.    //   
  152.    getLeft : function () /*:int*/ {   
  153.   
  154.      var oNode = this.textInput;   
  155.      var iLeft = 0;   
  156.      while(oNode.tagName != "BODY") {   
  157.        iLeft += oNode.offsetLeft;   
  158.        oNode = oNode.offsetParent;           
  159.     }   
  160.        
  161.     return iLeft;   
  162.   },   
  163.    getTop : function () /*:int*/ {   
  164.   
  165.      var oNode = this.textInput;   
  166.      var iTop = 0;   
  167.        
  168.      while(oNode.tagName != "BODY") {   
  169.         iTop += oNode.offsetTop;   
  170.         oNode = oNode.offsetParent;   
  171.      }   
  172.        
  173.      return iTop;   
  174.    },   
  175.     //显示层   
  176.    showSuggestions: function() {        
  177.       var divStyle = this.suggestionsDiv.style;    
  178.       if( divStyle.display != ''){   
  179.          this.positionSuggestionsDiv();               
  180.          divStyle.display = '';            
  181.       }                      
  182.       if(isIE){   
  183.          //创建与弹出div相同样式的iframe,来修复IE6及以下版本中div不能遮住select控件的bug            
  184.          if(!this.iframeSuggestions){   
  185.             this.iframeSuggestions = document.createElement("iframe");    
  186.             var iframeStyle = this.iframeSuggestions.style;    
  187.             iframeStyle.position ='absolute';   
  188.             iframeStyle.zIndex = 100;            
  189.             iframeStyle.top = divStyle.top;   
  190.             iframeStyle.left = divStyle.left;   
  191.             iframeStyle.width = divStyle.width;   
  192.             iframeStyle.height = this.suggestionsDiv.offsetHeight;                
  193.             iframeStyle.display = '';                    
  194.             this.textInput.parentNode.appendChild(this.iframeSuggestions);     
  195.          }             
  196.          var iframeStyle = this.iframeSuggestions.style;    
  197.          iframeStyle.position ='absolute';   
  198.          iframeStyle.zIndex = 100;            
  199.          iframeStyle.top = divStyle.top;   
  200.          iframeStyle.left = divStyle.left;   
  201.          iframeStyle.width = divStyle.width;   
  202.          iframeStyle.height = this.suggestionsDiv.offsetHeight;                
  203.          iframeStyle.display = '';            
  204.       }          
  205.          
  206.    },   
  207.     //定位层   
  208.    positionSuggestionsDiv: function() {         
  209.       var top = (this.getTop()+this.textInput.offsetHeight) ;   
  210.       var left = this.getLeft() ;   
  211.       var width = this.textInput.offsetWidth ;      
  212.           
  213.       var divStyle  = this.suggestionsDiv.style;   
  214.       divStyle.top  = top  + "px";   
  215.       divStyle.left = left + "px";   
  216.         
  217.       if ( this.options.matchTextWidth ){   
  218.         divStyle.width = width+ "px";    
  219.       }        
  220.        
  221.    },     
  222.     //隐藏层   
  223.    hideSuggestions: function() {   
  224.       this.suggestionsDiv.style.display = 'none';   
  225.       if(isIE){    
  226.         if(this.iframeSuggestions)   
  227.            this.iframeSuggestions.style.display = 'none';          
  228.       }         
  229.    },   
  230.     //创建层   
  231.    createSuggestionsDiv: function() {        
  232.         
  233.       this.suggestionsDiv = document.createElement("div");   
  234.       this.suggestionsDiv.className = this.options.suggestDivClassName;   
  235.          
  236.       var divStyle = this.suggestionsDiv.style;   
  237.       divStyle.position = 'absolute';   
  238.       divStyle.zIndex   = 101;   
  239.       divStyle.display  = "none";   
  240.       this.textInput.parentNode.appendChild(this.suggestionsDiv);            
  241.    },   
  242.     //更新层   
  243.    updateSuggestionsDiv: function() {   
  244.       this.suggestionsDiv.innerHTML = "";        
  245.       //如果有hidden变量,清空隐藏字段的值   
  246.      if(this.options.hiddenId != "" && this.options.hiddenId.length >0){   
  247.         $(this.options.hiddenId).value ="";   
  248.      }         
  249.       var suggestLines = this.createSuggestionSpans();   
  250.       for ( var i = 0 ; i < suggestLines.length ; i++ ){   
  251.         this.suggestionsDiv.appendChild(suggestLines[i]);          
  252.       }          
  253.    },   
  254.     //创建层中的选项span   
  255.    createSuggestionSpans: function() {   
  256.       var regExpFlags = "";   
  257.       if ( this.options.ignoreCase )   
  258.          regExpFlags = 'i';   
  259.       var startRegExp = "^";   
  260.       if ( this.options.matchAnywhere )   
  261.          startRegExp = '';   
  262.          //正则表达式匹配   
  263.       var regExp  = new RegExp( startRegExp + this.lastRequestString, regExpFlags );   
  264.       var suggestionSpans = new Array();   
  265.       for ( var i = 0 ; i < this.suggestions.length && i<this.options.count ; i++ )   
  266.          suggestionSpans.push( this.createSuggestionSpan( i, regExp ) )   
  267.   
  268.       return suggestionSpans;   
  269.    },   
  270.     //创建单个选项span   
  271.    createSuggestionSpan: function( n, regExp ) {   
  272.       var suggestion = this.suggestions[n];   
  273.   
  274.       var suggestionSpan = document.createElement("span");   
  275.       suggestionSpan.className = this.options.suggestionClassName;   
  276.       suggestionSpan.style.width   = '100%';   
  277.       suggestionSpan.style.display = 'block';   
  278.       suggestionSpan.id            = this.id + "_" + n;   
  279.       suggestionSpan.onmouseover   = this.mouseoverHandler.bindAsEventListener(this);   
  280.       suggestionSpan.onclick       = this.itemClickHandler.bindAsEventListener(this);   
  281.       var textValues = this.splitTextValues( suggestion.districtName+"",   
  282.                                              this.lastRequestString.length,   
  283.                                              regExp );   
  284.       var textMatchSpan = document.createElement("span");   
  285.       textMatchSpan.id            = this.id + "_match_" + n;   
  286.       textMatchSpan.className     = this.options.matchClassName;   
  287.       textMatchSpan.onmouseover   = this.mouseoverHandler.bindAsEventListener(this);   
  288.       textMatchSpan.onclick       = this.itemClickHandler.bindAsEventListener(this);   
  289.   
  290.       textMatchSpan.appendChild( document.createTextNode(textValues.mid) );   
  291.   
  292.       suggestionSpan.appendChild( document.createTextNode( textValues.start ) );   
  293.       suggestionSpan.appendChild( textMatchSpan );   
  294.       suggestionSpan.appendChild( document.createTextNode( textValues.end ) );   
  295.          
  296.       //把小区地址显示在小区名后面        
  297.       //suggestionSpan.appendChild(document.createTextNode("        "+suggestion.address ) );   
  298.       suggestionSpan.innerHTML+="    "+suggestion.address;   
  299.       return suggestionSpan;   
  300.    },   
  301.     //鼠标经过处理   
  302.    mouseoverHandler: function(e) {   
  303.       var src = e.srcElement ? e.srcElement : e.target;   
  304.       var index = parseInt(src.id.substring(src.id.lastIndexOf('_')+1));   
  305.       this.updateSelection(index);   
  306.    },   
  307.     //鼠标点击处理   
  308.    itemClickHandler: function(e) {   
  309.       this.mouseoverHandler(e);   
  310.       //鼠标点击把数据set入输入框!   
  311.       this.setInputFromSelection();   
  312.       this.hideSuggestions();   
  313.    },   
  314.     //分拆字符串   
  315.    splitTextValues: function( text, len, regExp ) {   
  316.       var startPos  = text.search(regExp);   
  317.       var matchText = text.substring( startPos, startPos + len );   
  318.       var startText = startPos == 0 ? "" : text.substring(0, startPos);   
  319.       var endText   = text.substring( startPos + len );   
  320.       return { start: startText, mid: matchText, end: endText };   
  321.    },   
  322.       
  323.    //如果只需要显示列表里的文字而已 可以调用此方法   
  324.    getElementContent: function(element) {   
  325.       return element.firstChild.data;   
  326.    }   
  327. };  
js 代码
  1. TextSuggest = Class.create();   
  2.   
  3. TextSuggest.prototype = {   
  4.     //构造函数   
  5.    initialize: function(elementId /*:HTMLInputElementId*/,oProvider /*:SuggestionProvider*/, options/*:cssStyle and display count  ect*/) {   
  6.       this.id          = elementId;        
  7.       this.provider    = oProvider;   
  8.       var browser      = navigator.userAgent.toLowerCase();   
  9.       this.isIE        = browser.indexOf("msie") != -1;   
  10.       this.isOpera     = browser.indexOf("opera")!= -1;   
  11.       this.isMozilla   = (browser.indexOf("gecko")!= -1 && browser.indexOf("mozilla")!= -1);   
  12.       this.textInput   = $(this.id);          
  13.       this.suggestions = new Array();   
  14.       this.setOptions(options);   
  15.       this.injectSuggestBehavior();   
  16.    },   
  17.     //设置参数   
  18.    setOptions: function(options) {   
  19.       this.options = {   
  20.          //层样式   
  21.          suggestDivClassName: 'suggestDiv',   
  22.          //选项样式   
  23.          suggestionClassName: 'suggestion',   
  24.          //匹配样式   
  25.          matchClassName     : 'match',   
  26.          //是否匹配输入框宽度   
  27.          matchTextWidth     : true,   
  28.          //选项颜色   
  29.          selectionColor     : '#b1c09c',   
  30.           //是否从头匹配   
  31.          matchAnywhere      : false,   
  32.          //是否忽略大小写   
  33.          ignoreCase         : false,   
  34.          //显示数目   
  35.          count              : 10,   
  36.           //隐藏字段的Id    
  37.          hiddenId           : ''                 
  38.       }.extend(options || {});   
  39.    },   
  40.     //注入输入提示行为   
  41.    injectSuggestBehavior: function() {   
  42.              
  43.       if ( this.isIE ||this.isMozilla){             
  44.           this.textInput.setAttribute('autocomplete','off');             
  45.       }   
  46.             
  47.     //创建控制器   
  48.       var keyEventHandler = new TextSuggestKeyHandler(this);   
  49.       //主要是为了避免在按回车的时候把表单提交 目前未能实现   
  50.       //new Insertion.After( this.textInput,'' );   
  51.      // new Insertion.After( this.textInput,'' );       
  52.       //创建div层   
  53.       this.createSuggestionsDiv();   
  54.    },      
  55.     //处理输入信息   
  56.    handleTextInput: function() {        
  57.      var previousRequest  = this.lastRequestString;        
  58.      this.lastRequestString =this.textInput.value.replace(/(^/s*)|(/s*$)/g, "");          
  59.      if ( this.lastRequestString == "" ||this.lastRequestString.length <=0 ){   
  60.           //如果有hidden的变量,要将变量置为空   
  61.          if(this.options.hiddenId != "" && this.options.hiddenId.length >0){               
  62.             $(this.options.hiddenId).value = "";                
  63.            }                   
  64.          this.hideSuggestions();    
  65.      }else if ( this.lastRequestString != previousRequest ){       
  66.           //访问数据源               
  67.         this.sendRequestForSuggestions();    
  68.      }            
  69.    },   
  70.     //选择框上移   
  71.    moveSelectionUp: function() {   
  72.       if ( this.selectedIndex > 0 ) {   
  73.          this.updateSelection(this.selectedIndex - 1);   
  74.       }   
  75.    },   
  76.     //选择框下移   
  77.    moveSelectionDown: function() {   
  78.       if ( this.selectedIndex < (this.suggestions.length - 1)  ) {   
  79.          this.updateSelection(this.selectedIndex + 1);   
  80.       }   
  81.    },   
  82.     //更新当前选择信息   
  83.    updateSelection: function(n) {   
  84.       var span = $( this.id + "_" + this.selectedIndex );   
  85.       if ( span ){   
  86.           //消除以前的样式   
  87.          span.style.backgroundColor = "";   
  88.       }   
  89.       this.selectedIndex = n;   
  90.       var span = $( this.id + "_" + this.selectedIndex );   
  91.       if ( span ){   
  92.           //更新新样式   
  93.          span.style.backgroundColor = this.options.selectionColor;   
  94.       }   
  95.    },   
  96.     //发送请求   
  97.    sendRequestForSuggestions: function() {   
  98.      if ( this.handlingRequest ) {   
  99.         this.pendingRequest = true;   
  100.         return;   
  101.      }   
  102.   
  103.      this.handlingRequest = true;   
  104.      this.callDWRAjaxEngine();   
  105.    },   
  106.   
  107.     //使用DWR访问后台   
  108.    callDWRAjaxEngine: function() {   
  109.            //清空以前的记录   
  110.            if(this.suggestions.length>0){   
  111.              this.suggestions = null;   
  112.            }             
  113.            //保存当前对象指针   
  114.            var tempThis = this;              
  115.            this.provider(this.lastRequestString,function(ajaxResponse){              
  116.             tempThis.suggestions = ajaxResponse;               
  117.               if ( tempThis.suggestions.length == 0 ) {   
  118.                   //如果有hidden的变量,要将变量置为空   
  119.                  if(tempThis.options.hiddenId != "" && tempThis.options.hiddenId.length >0){   
  120.                      $(tempThis.options.hiddenId).value = "";   
  121.                  }                   
  122.                  tempThis.hideSuggestions();                    
  123.                    
  124.               }else {                   
  125.                  tempThis.updateSuggestionsDiv();   
  126.                  tempThis.showSuggestions();   
  127.                  tempThis.updateSelection(0);                    
  128.               }   
  129.               tempThis.handlingRequest = false;   
  130.             if ( tempThis.pendingRequest ) {                     
  131.                  tempThis.pendingRequest    = false;                                                  
  132.                  tempThis.lastRequestString = this.textInput.value;                                  
  133.                  tempThis.sendRequestForSuggestions();   
  134.               }   
  135.            });   
  136.    },   
  137.    //显示信息   
  138.    setInputFromSelection: function() {        
  139.      var suggestion  = this.suggestions[ this.selectedIndex ];          
  140.      if(typeof(suggestion) != 'undefined'){   
  141.            //如果有hidden的变量,要将变量置赋值   
  142.           if(this.options.hiddenId != "" && this.options.hiddenId.length >0){          
  143.             $(this.options.hiddenId).value = suggestion.id;               
  144.            }            
  145.           this.textInput.value = suggestion.districtName;   
  146.                 
  147.        }        
  148.      this.hideSuggestions();        
  149.    },   
  150.       
  151.    //   
  152.    getLeft : function () /*:int*/ {   
  153.   
  154.      var oNode = this.textInput;   
  155.      var iLeft = 0;   
  156.      while(oNode.tagName != "BODY") {   
  157.        iLeft += oNode.offsetLeft;   
  158.        oNode = oNode.offsetParent;           
  159.     }   
  160.        
  161.     return iLeft;   
  162.   },   
  163.    getTop : function () /*:int*/ {   
  164.   
  165.      var oNode = this.textInput;   
  166.      var iTop = 0;   
  167.        
  168.      while(oNode.tagName != "BODY") {   
  169.         iTop += oNode.offsetTop;   
  170.         oNode = oNode.offsetParent;   
  171.      }   
  172.        
  173.      return iTop;   
  174.    },   
  175.     //显示层   
  176.    showSuggestions: function() {        
  177.       var divStyle = this.suggestionsDiv.style;    
  178.       if( divStyle.display != ''){   
  179.          this.positionSuggestionsDiv();               
  180.          divStyle.display = '';            
  181.       }                      
  182.       if(isIE){   
  183.          //创建与弹出div相同样式的iframe,来修复IE6及以下版本中div不能遮住select控件的bug            
  184.          if(!this.iframeSuggestions){   
  185.             this.iframeSuggestions = document.createElement("iframe");    
  186.             var iframeStyle = this.iframeSuggestions.style;    
  187.             iframeStyle.position ='absolute';   
  188.             iframeStyle.zIndex = 100;            
  189.             iframeStyle.top = divStyle.top;   
  190.             iframeStyle.left = divStyle.left;   
  191.             iframeStyle.width = divStyle.width;   
  192.             iframeStyle.height = this.suggestionsDiv.offsetHeight;                
  193.             iframeStyle.display = '';                    
  194.             this.textInput.parentNode.appendChild(this.iframeSuggestions);     
  195.          }             
  196.          var iframeStyle = this.iframeSuggestions.style;    
  197.          iframeStyle.position ='absolute';   
  198.          iframeStyle.zIndex = 100;            
  199.          iframeStyle.top = divStyle.top;   
  200.          iframeStyle.left = divStyle.left;   
  201.          iframeStyle.width = divStyle.width;   
  202.          iframeStyle.height = this.suggestionsDiv.offsetHeight;                
  203.          iframeStyle.display = '';            
  204.       }          
  205.          
  206.    },   
  207.     //定位层   
  208.    positionSuggestionsDiv: function() {         
  209.       var top = (this.getTop()+this.textInput.offsetHeight) ;   
  210.       var left = this.getLeft() ;   
  211.       var width = this.textInput.offsetWidth ;      
  212.           
  213.       var divStyle  = this.suggestionsDiv.style;   
  214.       divStyle.top  = top  + "px";   
  215.       divStyle.left = left + "px";   
  216.         
  217.       if ( this.options.matchTextWidth ){   
  218.         divStyle.width = width+ "px";    
  219.       }        
  220.        
  221.    },     
  222.     //隐藏层   
  223.    hideSuggestions: function() {   
  224.       this.suggestionsDiv.style.display = 'none';   
  225.       if(isIE){    
  226.         if(this.iframeSuggestions)   
  227.            this.iframeSuggestions.style.display = 'none';          
  228.       }         
  229.    },   
  230.     //创建层   
  231.    createSuggestionsDiv: function() {        
  232.         
  233.       this.suggestionsDiv = document.createElement("div");   
  234.       this.suggestionsDiv.className = this.options.suggestDivClassName;   
  235.          
  236.       var divStyle = this.suggestionsDiv.style;   
  237.       divStyle.position = 'absolute';   
  238.       divStyle.zIndex   = 101;   
  239.       divStyle.display  = "none";   
  240.       this.textInput.parentNode.appendChild(this.suggestionsDiv);            
  241.    },   
  242.     //更新层   
  243.    updateSuggestionsDiv: function() {   
  244.       this.suggestionsDiv.innerHTML = "";        
  245.       //如果有hidden变量,清空隐藏字段的值   
  246.      if(this.options.hiddenId != "" && this.options.hiddenId.length >0){   
  247.         $(this.options.hiddenId).value ="";   
  248.      }         
  249.       var suggestLines = this.createSuggestionSpans();   
  250.       for ( var i = 0 ; i < suggestLines.length ; i++ ){   
  251.         this.suggestionsDiv.appendChild(suggestLines[i]);          
  252.       }          
  253.    },   
  254.     //创建层中的选项span   
  255.    createSuggestionSpans: function() {   
  256.       var regExpFlags = "";   
  257.       if ( this.options.ignoreCase )   
  258.          regExpFlags = 'i';   
  259.       var startRegExp = "^";   
  260.       if ( this.options.matchAnywhere )   
  261.          startRegExp = '';   
  262.          //正则表达式匹配   
  263.       var regExp  = new RegExp( startRegExp + this.lastRequestString, regExpFlags );   
  264.       var suggestionSpans = new Array();   
  265.       for ( var i = 0 ; i < this.suggestions.length && i<this.options.count ; i++ )   
  266.          suggestionSpans.push( this.createSuggestionSpan( i, regExp ) )   
  267.   
  268.       return suggestionSpans;   
  269.    },   
  270.     //创建单个选项span   
  271.    createSuggestionSpan: function( n, regExp ) {   
  272.       var suggestion = this.suggestions[n];   
  273.   
  274.       var suggestionSpan = document.createElement("span");   
  275.       suggestionSpan.className = this.options.suggestionClassName;   
  276.       suggestionSpan.style.width   = '100%';   
  277.       suggestionSpan.style.display = 'block';   
  278.       suggestionSpan.id            = this.id + "_" + n;   
  279.       suggestionSpan.onmouseover   = this.mouseoverHandler.bindAsEventListener(this);   
  280.       suggestionSpan.onclick       = this.itemClickHandler.bindAsEventListener(this);   
  281.       var textValues = this.splitTextValues( suggestion.districtName+"",   
  282.                                              this.lastRequestString.length,   
  283.                                              regExp );   
  284.       var textMatchSpan = document.createElement("span");   
  285.       textMatchSpan.id            = this.id + "_match_" + n;   
  286.       textMatchSpan.className     = this.options.matchClassName;   
  287.       textMatchSpan.onmouseover   = this.mouseoverHandler.bindAsEventListener(this);   
  288.       textMatchSpan.onclick       = this.itemClickHandler.bindAsEventListener(this);   
  289.   
  290.       textMatchSpan.appendChild( document.createTextNode(textValues.mid) );   
  291.   
  292.       suggestionSpan.appendChild( document.createTextNode( textValues.start ) );   
  293.       suggestionSpan.appendChild( textMatchSpan );   
  294.       suggestionSpan.appendChild( document.createTextNode( textValues.end ) );   
  295.          
  296.       //把小区地址显示在小区名后面        
  297.       //suggestionSpan.appendChild(document.createTextNode("        "+suggestion.address ) );   
  298.       suggestionSpan.innerHTML+="    "+suggestion.address;   
  299.       return suggestionSpan;   
  300.    },   
  301.     //鼠标经过处理   
  302.    mouseoverHandler: function(e) {   
  303.       var src = e.srcElement ? e.srcElement : e.target;   
  304.       var index = parseInt(src.id.substring(src.id.lastIndexOf('_')+1));   
  305.       this.updateSelection(index);   
  306.    },   
  307.     //鼠标点击处理   
  308.    itemClickHandler: function(e) {   
  309.       this.mouseoverHandler(e);   
  310.       //鼠标点击把数据set入输入框!   
  311.       this.setInputFromSelection();   
  312.       this.hideSuggestions();   
  313.    },   
  314.     //分拆字符串   
  315.    splitTextValues: function( text, len, regExp ) {   
  316.       var startPos  = text.search(regExp);   
  317.       var matchText = text.substring( startPos, startPos + len );   
  318.       var startText = startPos == 0 ? "" : text.substring(0, startPos);   
  319.       var endText   = text.substring( startPos + len );   
  320.       return { start: startText, mid: matchText, end: endText };   
  321.    },   
  322.       
  323.    //如果只需要显示列表里的文字而已 可以调用此方法   
  324.    getElementContent: function(element) {   
  325.       return element.firstChild.data;   
  326.    }   
  327. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值