yui --------autocomplete學習二

          Posted on 2007-05-24 02:20 yoyo 閱讀(1951) 評論(3)  編輯  收藏 所屬分類: ajax
          ??????今天又研究了一下autocomplete,終于又有一些些進步了。終于動態地調用遠程的文件來改變選擇欄的內容....返回來的格式是Flat-file Data ,,,不知道怎么翻譯好。大概就是文本格式..其實返回來xml,json格式會更好處理。

          1oACDS?=?new?YAHOO.widget.DS_XHR("./php/ysearch_flat.php",?["\n",?"\t"]);???
          2?oACDS.responseType?=?YAHOO.widget.DS_XHR.TYPE_FLAT;???
          3?oACDS.maxCacheEntries?=?60;???
          4?oACDS.queryMatchSubset?=?true;???
          5



          "./php/ysearch_flat.php" 這里是要調用的服務端請求.第二個參數就是要分隔的行數
          (
          ???需要說明的是輸入框要用這種格式
          ????????????<div?id="ysearchautocomplete0">
          ????????????????
          <input?id="ysearchinput0">
          ????????????????
          <div?id="ysearchcontainer0"></div>
          ????????????
          </div>
          其中ysearchcontainer0就是用來放下拉選擇的數據
          而發送的請求,yui都會自做主張的加上一個?query='+ysearchinput的值
          )

          看看和上一篇的oACDS定義來說.這是一種new YAHOO.widget.DS_XHR
          因為這是需要遠程調用所以需要用到connection.js這個js文件,所以一定要把它加進來
          粗略看過YAHOO.widget.DS_XHR這個類,它還是需要處理服務器返回來的數據,所以要定義responseType的屬性值。。其它兩個屬性設置可有可無

          接著就寫下以下兩行代碼
          oAutoComp2?=?new?YAHOO.widget.AutoComplete('ysearchinput2','ysearchcontainer2',?oACDS);???
          50?oAutoComp2.delimChar?=?";";???
          51?oAutoComp2.queryDelay?=?0;???
          52?oAutoComp2.prehighlightClassName?=?"yui-ac-prehighlight";??
          queryDelya屬性就是每隔幾秒發送一次請求..因為需要向服務器發生請求,可能過大的消耗服務器資源.

          需要說明的因為ajax都是以utf-8的方式進行處理數據的。所以返回的數據如果utf-8編碼的話當然就沒有問題啦。。但如果是其它的編碼的話.?
          就要在html響應中加上編碼格式charset="gb2312",php寫法是
          header("charset=gb2312")

          Feedback

          # re: yui --------autocomplete學習二  回復  更多評論   

          2007-11-03 01:11 by daiqiaobin
          我正要用yui做開發
          服務器端:

          AutoComp.java-------------------------------

          package ajaxdemo1;

          import javax.servlet.*;
          import javax.servlet.http.*;
          import java.io.*;
          import java.util.*;

          public class AutoComp extends HttpServlet {
          private static final String CONTENT_TYPE = "text/xml; charset=UTF-8";

          //Initialize global variables
          public void init() throws ServletException {
          }

          //Process the HTTP Get request
          public void doGet(HttpServletRequest request, HttpServletResponse response) throws
          ServletException, IOException {
          response.setContentType(CONTENT_TYPE);
          //務必清空頁面緩存
          response.addHeader("Cache-Control","no-cache");

          System.out.println("url = " + request.getRequestURL() + "?" + request.getQueryString());
          String query = request.getParameter("query");

          PrintWriter out = response.getWriter();
          out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
          out.print("<Resultset>");
          for(int i=0;i<40;i++){
          out.print("<Result>");
          out.print("<Title>title"+i+"</Title>");
          out.print("<Summary>Hello World"+i+"!</Summary>");
          out.print("</Result>");
          }
          out.print("</Resultset>");
          out.close();
          }

          //Process the HTTP Post request
          public void doPost(HttpServletRequest request, HttpServletResponse response) throws
          ServletException, IOException {
          doGet(request, response);
          }

          //Clean up resources
          public void destroy() {
          }
          }

          index.jsp================================

          <%@ page contentType="text/html; charset=GBK" %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <title>AJAX測試文件</title>

          <link rel="stylesheet" type="text/css" href="build/fonts/fonts-min.css" />
          <link rel="stylesheet" type="text/css" href="build/autocomplete/assets/skins/sam/autocomplete.css" />
          <script type="text/javascript" src="build/yahoo-dom-event/yahoo-dom-event.js"></script>
          <script type="text/javascript" src="build/animation/animation.js"></script>
          <script type="text/javascript" src="build/autocomplete/autocomplete.js"></script>
          <script type="text/javascript" src="build/yahoo/yahoo.js"></script>
          <script type="text/javascript" src="build/event/event.js"></script>
          <script type="text/javascript" src="build/connection/connection.js"></script>

          <!--begin custom header content for this example-->
          <style type="text/css">
          /* custom styles for this example */
          #ysearchautocomplete{
          margin-bottom:2em;
          width:25em;
          }
          </style>

          </head>
          <body class="yui-skin-sam">

          <form action="http://localhost:8080/ajax" onsubmit="return YAHOO.example.ACXml.validateForm();">
          <h3>Yahoo! Search:</h3>
          <div id="ysearchautocomplete">
          <input id="ysearchinput" type="text" name="p">
          <div id="ysearchcontainer"></div>
          </div>
          </form>

          <script type="text/javascript" >
          YAHOO.example.ACXml = new function(){

          // Instantiate an XHR DataSource and define schema as an array:
          // ["Multi-depth.object.notation.to.find.a.single.result.item",
          // "Query Key",
          // "Additional Param Name 1",
          // ...
          // "Additional Param Name n"]

          this.oACDS = new YAHOO.widget.DS_XHR("http://localhost:8080/ajax/autocomp", ["Result", "Title", "Summary"]);
          this.oACDS.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
          //this.oACDS.queryMatchContains = true;
          //this.oACDS.scriptQueryAppend = ""; // Needed for YWS
          // Instantiate AutoComplete

          this.oAutoComp = new YAHOO.widget.AutoComplete("ysearchinput","ysearchcontainer", this.oACDS);

          // Display up to 20 results in the container
          this.oAutoComp.maxResultsDisplayed = 10;

          // Require user to type at least 1 characters before triggering a query
          this.oAutoComp.minQueryLength = 1;

          // Every key input event will trigger an immediate query...
          this.oAutoComp.queryDelay = 0;

          // ...or queries will be sent after 3 seconds have passed
          // since the last key input event
          //this.oAutoComp.queryDelay = 3;
          // Do not automatically highlight the first result item in the container
          this.oAutoComp.autoHighlight = false;

          // Enable a drop-shadow under the container element
          this.oAutoComp.useShadow = true;

          // Enable an iFrame shim under the container element
          this.oAutoComp.useIFrame = true;

          this.oAutoComp.formatResult = function(oResultItem, sQuery) {
          return oResultItem[0] + " " + oResultItem[1];
          };


          // Stub for AutoComplete form validation
          this.validateForm = function() {
          // Validation code goes here
          return true;

          };

          };
          </script>

          </body>
          </html>

          # re: yui --------autocomplete學習二  回復  更多評論   

          2007-11-03 01:14 by daiqiaobin
          以上代碼報錯:
          this.connMgr 為空或不是對象
          請問樓主是怎么回事啊?

          # re: yui --------autocomplete學習二  回復  更多評論   

          2008-08-05 13:59 by raul86
          我現在在用這個東西,但是不知道調到Action之后如何返回數據。
          能不能給我一份動態查詢的例子,郵箱是raul_1986@126.com.

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          posts - 2, comments - 4, trackbacks - 0, articles - 4

          Copyright © yoyo

          主站蜘蛛池模板: 陆良县| 宝山区| 五莲县| 右玉县| 磐安县| 白山市| 怀集县| 丰台区| 渑池县| 高雄市| 镇平县| 日喀则市| 五大连池市| 兰坪| 秦安县| 新平| 金塔县| 望谟县| 北流市| 禹州市| 宜都市| 佳木斯市| 景谷| 敦煌市| 郯城县| 叶城县| 金昌市| 永丰县| 榆树市| 镇康县| 漳平市| 丹棱县| 长岭县| 西华县| 社会| 朔州市| 辰溪县| 凤翔县| 青铜峡市| 潜山县| 潜江市|