yui --------autocomplete学习二

Posted on 2007-05-24 02:20 yoyo 阅读(1938) 评论(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