js自動補全——接上一篇JavaScript自動補全

在"JavaScript自動補全"這篇文章中、用一個數組把"顏色集合"存儲起來、而後在跟用戶輸入的值進行對比、此次呢把數組的值替換掉。用Ajax與後臺數據進行交互、前面的代碼就很少說了。數據庫呢、用的是Oracle10G。js呢、用了一個框架、就是Jquery(1.4.2)。後臺框架採用的是ssh~~~這個ssho(︶︿︶)o 唉!!!javascript

我寫的這個自動補全呢、基本上是個廢品、但願看到得能說說本身對自動補全的一些見解、不管是Sql語句、仍是數據庫的設計、而後程序的設計等等。html

數據庫設計:java

  
  
           
  
  
  1. --建立表、id爲主鍵自動增加、title爲用戶輸入的關鍵字、clicks爲這個關鍵字的點擊率   
  2. create table bms_mate(id integer primary key ,title varchar2(50),clicks number);   
  3.  
  4. -- 創建序列:   
  5. -- Create sequence    
  6. create sequence bms_mate_sequence                          
  7. minvalue 1   --最小               
  8. maxvalue 999999999999999999999999999  --最大   
  9. start with 1   --增長量   
  10. increment by 1 --從***開始   
  11. cache 20;      
  12.  
  13.  insert into bms_mate values(bms_mate_sequence.nextval,'旦旦而學')   
  14.     
  15.  select * from bms_mate 
數據截圖:

後臺代碼:Dao層、node

  
  
           
  
  
  1. /**  
  2.  * 帶條件查詢  
  3.  */   
  4. public List<T> getAll(String hql,List params) {   
  5.     query = this.getSession().createQuery(hql);   
  6.     if (null != params && params.size() > 0) {   
  7.         // 循環給參數賦值   
  8.         for (int i = 0; i < params.size(); i++) {   
  9.             query.setParameter(i, params.get(i));   
  10.         }   
  11.     }   
  12.     List<T> list = query.list();   
  13.     return list;   
Biz層

  
  
           
  
  
  1. public List getBmsAll(String title) throws Exception {   
  2.   String hql = "select title from BmsMate where title like ? order by clicks desc";   
  3.   List params = new ArrayList();   
  4.   params.add(title+"%");   
  5.   return baseDao.getAll(hql,params);   
  6.  }  

Actionjquery

  
  
           
  
  
  1. package com.boxun.action;   
  2.    
  3. import java.io.PrintWriter;   
  4.    
  5. import java.util.List;   
  6.    
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9.    
  10. import org.apache.struts2.interceptor.ServletRequestAware;   
  11. import org.apache.struts2.interceptor.ServletResponseAware;   
  12.    
  13. import com.boxun.biz.IAjaxBiz;   
  14. import com.opensymphony.xwork2.ActionSupport;   
  15.    
  16. public class AjaxAtion extends ActionSupport implements ServletRequestAware , ServletResponseAware{   
  17.     /* HttpServletRequest對象  
  18.      */   
  19.     private javax.servlet.http.HttpServletRequest request;   
  20.     private javax.servlet.http.HttpServletResponse response;   
  21.        
  22.     private IAjaxBiz ajaxBiz;   
  23.    
  24.     public void setAjaxBiz(IAjaxBiz ajaxBiz) {   
  25.         this.ajaxBiz = ajaxBiz;   
  26.     }   
  27.     public void setServletRequest(HttpServletRequest request) {   
  28.         this.request = request;   
  29.     }   
  30.     public void setServletResponse(HttpServletResponse response) {   
  31.         this.response=response;   
  32.     }   
  33.        
  34.     /**  
  35.      * 查詢匹配項  
  36.      * 2011-7-13上午10:05:37  
  37.      * @return  
  38.      */   
  39.     public String getTitle(){   
  40.         PrintWriter out = null;   
  41.         response.setCharacterEncoding("UTF-8");   
  42.         try{   
  43.             out = response.getWriter();   
  44.             String title = request.getParameter("colors");   
  45.             if(title == null || title.equals("")){   
  46.                 out.print("error");   
  47.                 return null;   
  48.             }   
  49.             List list = ajaxBiz.getBmsAll(title);   
  50.             String result = "" ;   
  51.             for (Object obj : list) {   
  52.                 result += ","+obj.toString();   
  53.             }   
  54.             if(result != null && !"".equals(result))   
  55.                 out.print(result.substring(1));   
  56.             else   
  57.                 out.print("");   
  58.         }catch(Exception ex){   
  59.             out.print("error");   
  60.             ex.printStackTrace();   
  61.         }finally{   
  62.             if(out != null) out.close();   
  63.         }   
  64.         return null;   
  65.     }   
  66.        
  67. }  

Struts配置:ajax

  
  
           
  
  
  1. <action name="jqueryAjax" class="ajaxAction">   
  2.             <result></result>   
  3.         </action>  

頁面代碼:sql

  
  
           
  
  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.    
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <base href="<%=basePath%>">   
  11.        
  12.     <title>匹配用戶輸入</title>   
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">   
  15.     <meta http-equiv="cache-control" content="no-cache">   
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  18.     <meta http-equiv="description" content="This is my page">   
  19.     <style>   
  20.     <!--   
  21.     body{   
  22.         font-family: Arial,Helvetica,sans-serif;   
  23.         font-size: 12px; padding: 0px; margin: 5px;   
  24.     }   
  25.     form{padding: 0px; margin: 0px;}   
  26.     input{   
  27.         /*用戶輸入框的樣式*/   
  28.         font-family: Arial,Helvetica,sans-serif;   
  29.         font-size: 12px; border: 1px solid #000000;   
  30.         width: 200px; padding: 1px; margin: 0px;   
  31.     }   
  32.     #popup{   
  33.         /*提示框div塊的樣式*/   
  34.         position: absolute; width: 202px;   
  35.         color: #004a7e; font-size: 12px;   
  36.         font-family: Arial,Helvetica,sans-serif;   
  37.         left: 36px; top: 25px;   
  38.     }   
  39.     #popup.show{   
  40.         /*顯示提示框的邊框*/   
  41.         border: 1px solid #004a7e;   
  42.     }   
  43.     #popup.hide{   
  44.         /*隱藏提示框的邊框*/   
  45.         border: none;      
  46.     }   
  47.     /*提示框的樣式風格*/   
  48.     ul{   
  49.         list-style: none;   
  50.         margin: 0px; padding: 0px;   
  51.     }   
  52.     li.mouseOver{   
  53.         background-color: #004a7e;   
  54.         color: #FFFFFF;   
  55.     }   
  56.     li.mouseOut{   
  57.         background-color: #FFFFFF;   
  58.         color: #004a7e;   
  59.     }   
  60.     -->   
  61.     </style>   
  62.     <script type="text/javascript" src="js/jquery-1.4.4.js" ></script>   
  63.     <script type="text/javascript">   
  64.         var oInputField ,oPopDiv , oColorsUl,aColors;   
  65.            
  66.         function initVars(){   
  67.             //初始化變量   
  68.             oInputField = document.forms["myForm1"].colors;   
  69.             oPopDiv = document.getElementById("popup");   
  70.             oColorsUl = document.getElementById("colors_ul");   
  71.         }   
  72.         function findColors(){   
  73.             initVars();   
  74.             var aResult = new Array();  //用於存放匹配結果   
  75.             if(oInputField.value.length > 0){   
  76.                 var params = jQuery("#myForm1").serialize();  //序列化表格數據"myForm1"爲表格的id   
  77.                 $.ajax({   
  78.                     type:'post',   
  79.                     data: params ,   
  80.                     url:"<%=path %>/jqueryAjax!getTitle.action",   
  81.                     success:function(data) {   
  82.                         if(data == "error" || data == null || data == ""){   
  83.                             clearColors();   
  84.                             return;   
  85.                         }    
  86.                         aResult = data.split(",");   
  87.                         setColors(aResult);   
  88.                     }    
  89.                 });    
  90.             }   
  91.             else   
  92.                 clearColors(); //無輸入時清除提示框   
  93.         }   
  94.            
  95.            
  96.         function clearColors(){   
  97.             //清除提示內容   
  98.             for(var i = oColorsUl.childNodes.length - 1 ; i >= 0 ; i-- )   
  99.                 oColorsUl.removeChild(oColorsUl.childNodes[i]);   
  100.             oPopDiv.className = "hide" ;   
  101.         }   
  102.            
  103.         function setColors(the_colors){   
  104.             //顯示提示框、傳入的參數即爲匹配出來的結果組成的數組   
  105.             clearColors();  //沒輸入一個字母就先清除原先的提示、再繼續   
  106.             oPopDiv.className = "show" ;   
  107.             var oLi ;   
  108.             for(var i = 0 ; i < the_colors.length ; i++ ){   
  109.                 //將匹配的提示結果逐一顯示給用戶   
  110.                 oLi = document.createElement("li");   
  111.                 oColorsUl.appendChild(oLi);   
  112.                 oLi.appendChild(document.createTextNode(the_colors[i]));   
  113.                    
  114.                 oLi.onmouseover = function(){   
  115.                     this.className = "mouseOver" ;  //鼠標指針通過時高亮   
  116.                 }   
  117.                 oLi.onmouseout = function(){   
  118.                     this.className = "mouseOut" ;   //鼠標指針離開時恢復原樣   
  119.                 }   
  120.                 oLi.onclick = function(){   
  121.                     //用戶單擊某個匹配項時、設置輸入框爲該項的值   
  122.                     oInputField.value = this.firstChild.nodeValue;   
  123.                     clearColors();  //同時清除提示框   
  124.                 }   
  125.             }   
  126.         }   
  127.            
  128.     </script>   
  129.    
  130.   </head>   
  131.      
  132.   <body>   
  133.     <form method="post" name="myForm1" id="myForm1">   
  134.         Color:<input type="text" name="colors" id="colors" onkeyup="findColors();" />   
  135.     </form>   
  136.     <div id="popup">   
  137.         <ul id="colors_ul"></ul>   
  138.     </div>   
  139.   </body>   
  140. </html> 

運行截圖:數據庫

只是對findColors()方法進行了修改、其餘都不改動~~~!!!apache

在這裏點擊某個值的時候應該在去後臺給這個字段的clicks加1的、我尚未作、另一個就是若是我有幾百萬、幾千萬、甚至上億條數據的時候該怎麼辦!數組

這個玩意怎麼優化啊? 但願知道的給說說、感激涕零!!!

相關文章
相關標籤/搜索