JS 自动完成 AutoComplete(Ajax 查询)


Posted in Javascript onJuly 07, 2009

JS 自动完成 AutoComplete(Ajax 查询)
一:JS 部分

//******************************************************** 
//创建日期: 2009-03-10 
//作 者: oloen 
//?热菟得? 自动完成JS类 
//用法: 
// var auto = new autoComplete(客户端ID); 
// auto.Init(document.all.客户端ID); 
// auto.Type = 'PSUnit' //PSSale 
//******************************************************** 
//自动完成 
function autoComplete(id) 
{ 
var me = this; 
//自动完成绑定控件客户端ID 
me.AutoCompleteControlID 
me.handle = null 
me.DivResult ; 
me.currentIndex = -1; 
me.LastIndex = -1; 
me.requestObj; 
me.CurrentTD = ''; 
me.Filter = '1=1'; 
me.Type = 'PSUnit' 
if(id != null && typeof(id) != undefined) 
me.AutoCompleteControlID = id; 
if(me.DivResult == null||typeof(me.DivResult)=="undefined") 
{ 
me.DivResult = document.createElement("div"); 
var parent = document.getElementById(me.AutoCompleteControlID).parentElement; 
if(typeof(parent)!="undefined"){ 
parent.appendChild(me.DivResult); 
} 
} 
this.Init = function(obj) 
{ 
me.handle = obj 
me.AutoCompleteControlID = obj.id 
} 
this.Auto = function() 
{ 
me.DivResult.style.position = "absolute"; 
me.DivResult.style.top = me.handle.getBoundingClientRect().top - 11; 
me.DivResult.style.left = me.handle.getBoundingClientRect().left; 
me.DivResult.style.width = me.handle.width; 
me.DivResult.style.height = 20; 
me.DivResult.style.backgroundColor = "#ffffff"; 
//如果按下 向上, 向下 或 回车 
if (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 13) 
{ 
me.SelectItem(); 
} 
else 
{ 
//恢复下拉选择项为 -1 
currentIndex = -1; 
if(window.XMLHttpRequest) 
{ 
me.requestObj = new XMLHttpRequest(); 
if(me.requestObj.overrideMimeType) 
me.requestObj.overrideMimeType("text/xml"); 
} 
else if(window.ActiveXObject) 
{ 
try 
{ 
me.requestObj = new ActiveXObject("Msxml2.XMLHTTP"); 
} 
catch(e) 
{ 
me.requestObj = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
} 
if(me.requestObj == null) 
return; 
me.requestObj.onreadystatechange = me.ShowResult; 
me.requestObj.open("GET", "../Common/AutoComplete.aspx?InputValue=" + escape(me.handle.value) + "&Filter=" + me.Filter + "&Type=" + me.Type, true); 
me.requestObj.send(); 
} 
} 
this.ShowResult = function() 
{ 
if (me.requestObj.readyState == 4) 
{ 
me.DivResult.innerHTML = me.requestObj.responseText; 
me.DivResult.style.display = ""; 
} 
} 
this.SelectItem = function() 
{ 
//结果 
var result = document.getElementById("Tmp_AutoComplete_tblResult"); 
if (!result) 
return; 
if(result.rows[me.LastIndex] != null) 
{ 
result.rows[me.LastIndex].style.backgroundColor = "#FFFFFF"; 
result.rows[me.LastIndex].style.color = "#000000"; 
} 
var maxRow = result.rows.length; 
//向上 
if (event.keyCode == 38 && me.currentIndex > 0) 
me.currentIndex--; 
//向下 
if (event.keyCode == 40 && me.currentIndex < maxRow-1) 
me.currentIndex++; 
//回车 
if (event.keyCode == 13) 
{ 
me.Select() 
me.InitItem(); 
return; 
} 
if(result.rows[me.currentIndex]!=undefined) 
{ 
//选中颜色 
result.rows[me.currentIndex].style.backgroundColor = "#3161CE"; 
result.rows[me.currentIndex].style.color = "#FFFFFF"; 
} 
me.DivResult.style.height = maxRow * 15; 
me.LastIndex = me.currentIndex; 
} 
this.Select = function() 
{ 
var result = document.getElementById("Tmp_AutoComplete_tblResult"); 
if (!result || result.rows.length<=0) 
return; 
//默认第一条记录 
if(me.currentIndex < 0) 
me.currentIndex = 0; 
var ReturnValue = result.rows[me.currentIndex].ReturnValue; 
if(ReturnValue != undefined) 
{ 
me.DivResult.style.display = 'none'; 
//设置页面值 
ReturnAutoComplete(ReturnValue); 
} 
} 
this.Hide = function() 
{ 
me.DivResult.style.display = 'none'; 
me.currentIndex = -1; 
} 
this.InitItem = function() 
{ 
me.DivResult.style.display = 'none'; 
me.DivResult.innerHTML = ""; 
me.currentIndex = -1; 
} 
me.DivResult.onclick = function() 
{ 
try{me.Auto();}catch(e){} 
} 
document.getElementById(me.AutoCompleteControlID).onclick = function(){ 
try{me.Auto();}catch(e){} 
} 
document.getElementById(me.AutoCompleteControlID).onkeyup = function(){ 
try{me.Auto();}catch(e){} 
} 
document.getElementById(me.AutoCompleteControlID).onkeydown = function(){ 
if (event.keyCode == 13) 
{ 
try 
{ 
me.Select() 
me.InitItem(); 
}catch(e){} 
} 
} 
document.getElementById(me.AutoCompleteControlID).onblur = function(){ 
me.Hide(); 
} 
}

2 后台查询页面
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
//******************************************************** 
//创建日期: 2009-03-10 
//作 者: Oloen 
//?热菟得? 自动完成后台查询页面 
// Type:pssale 合同查询 
// Type:psunit 房间查询 
//******************************************************** 
/// <summary> 
/// 自动完成后台查询页面 
/// </summary> 
public partial class Common_AutoComplete : System.Web.UI.Page 
{ 
const string tbStyle = @"style=""color:#F7922E"""; 
/// <summary> 
/// 过滤条件 
/// </summary> 
string Filter = string.Empty; 
/// <summary> 
/// 查询值 
/// </summary> 
string InputValue = string.Empty; 
/// <summary> 
/// 自动完成类别 
/// Type:pssale 合同查询 
/// Type:psunit 房间查询 
/// </summary> 
string Type = string.Empty; 
/// <summary> 
/// 返回结果字符 
/// </summary> 
string ReturnStr = string.Empty; 
private void Page_Load(object sender, System.EventArgs e) 
{ 
switch (Type.ToLower()) 
{ 
case "pssale": 
case "psunit": 
default: 
AutoPSUnitNo(); 
break; 
} 
Response.Clear(); 
Response.ContentType = "text/xml"; 
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); 
Response.Write(ReturnStr); 
Response.End(); 
} 
protected override void OnInit(EventArgs e) 
{ 
base.OnInit(e); 
Filter = Request.QueryString["Filter"] ?? "1=1"; 
InputValue = Request.QueryString["InputValue"] ?? ""; 
InputValue.Replace("'","''"); 
Type = Request.QueryString["Type"] ?? ""; 
} 
/// <summary> 
/// 售楼系统房间编号自动完成 
/// </summary> 
void AutoPSUnitNo() 
{ 
if (!string.IsNullOrEmpty(InputValue)) 
{ 
ReturnStr = @"<table cellSpacing=""0"" cellPadding=""0"" width=""150px"" align=""center"" border=""0"" id=""Tmp_AutoComplete_tblResult"" style=""padding-left:5;padding-right:5; background-color:#FFFFFF;border:1px solid #999999;"">"; 
string Sql = string.Format(@"SELECT TOP 10 UnitID,UnitNo,ProjectNo,PhaseNo,BlockNo FROM View_PS_Unit WHERE UnitNo LIKE '%{0}%' AND {1}", InputValue, Filter); 
if (Type.ToLower().Equals("pssale")) 
Sql = string.Format(@"SELECT TOP 10 SaleID,UnitID,UnitNo,ContractNo,Name,SaleDate,SellingPrice,ProjectNo FROM View_PS_Sale_Unit WHERE UnitNo LIKE '%{0}%' AND {1}", InputValue, Filter); 
using (SqlDataReader sdr = DataAccessHelper.ExecuteReader(Sql) as SqlDataReader) 
{ 
if (sdr == null || !sdr.HasRows) 
{ 
ReturnStr = string.Empty; 
return; 
} 
while (sdr.Read()) 
{ 
string td = string.Format(@"<td height=""15"" nowrap>{0}</td>", sdr["ProjectNo"].ToString()); 
//td += string.Format(@"<td height=""15"" nowrap>{0}</td>", sdr["PhaseNo"].ToString()); 
//td += string.Format(@"<td height=""15"" nowrap>{0}</td>", sdr["BlockNo"].ToString()); 
td += string.Format(@"<td height=""15"" nowrap align=""right"" {1}>{0}</td>", sdr["UnitNo"].ToString(), tbStyle); 
if (Type.ToLower().Equals("pssale")) 
{ 
ReturnStr += string.Format(@"<tr ReturnValue=""{0},{1},{2},{3},{4},{5},{6}"">{7}</tr>", 
sdr["SaleID"].ToString(), sdr["UnitID"].ToString(), 
sdr["UnitNo"].ToString(), sdr["ContractNo"].ToString(), 
sdr["Name"].ToString(), sdr["SaleDate"].ToString(), 
sdr["SellingPrice"].ToString(), td); 
} 
else 
ReturnStr += string.Format(@"<tr ReturnValue=""{0},{1}"">{2}</tr>", sdr["UnitID"].ToString(), sdr["UnitNo"].ToString(), td); 
} 
} 
ReturnStr += @"</table>"; 
} 
} 
}

3 调用部分
<!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> 
<meta http-equiv="Content-Type" content="text/html; charset=gb-2312" /> 
<script type="text/javascript" src="../JS/AutoComplete.js"></script> 
<title>无标题 1</title> 
</head> 
<body> 
<input id="t1" type="text"> 
<script> 
var auto = new autoComplete('t1') 
auto.Init(document.all.t1); 
//选中后做的事情 
function ReturnAutoComplete(ReturnValue) 
{ 
alert(ReturnValue) 
} 
</script> 
</body> 
</html>
Javascript 相关文章推荐
javascript 45种缓动效果 非常酷
Jun 28 Javascript
jquery ajax 简单范例(界面+后台)
Nov 19 Javascript
JQuery表格拖动调整列宽效果(自己动手写的)
Sep 01 Javascript
EasyUI实现二级页面的内容勾选的方法
Mar 01 Javascript
一个字符串中出现次数最多的字符 统计这个次数【实现代码】
Apr 29 Javascript
Vue.js结合bootstrap实现分页控件
Mar 10 Javascript
webpack配置文件和常用配置项介绍
Apr 28 Javascript
javascript实现二叉树遍历的代码
Jun 08 Javascript
详解自定义ajax支持跨域组件封装
Feb 08 Javascript
vue-cli2与vue-cli3在一台电脑共存的实现方法
Sep 25 Javascript
vant组件中 dialog的确认按钮的回调事件操作
Nov 04 Javascript
如何将JavaScript将数组转为树形结构
Jun 02 Javascript
javascript 选择文件夹对话框(web)
Jul 07 #Javascript
JavaScript 解析读取XML文档 实例代码
Jul 07 #Javascript
javascript 新浪背投广告实现代码
Jul 07 #Javascript
JavaScript 对象、函数和继承
Jul 07 #Javascript
js 日期转换成中文格式的函数
Jul 07 #Javascript
javascript 面向对象思想 附源码
Jul 07 #Javascript
jquery BS,dialog控件自适应大小
Jul 06 #Javascript
You might like
PHP5.2下chunk_split()函数整数溢出漏洞 分析
2007/06/06 PHP
php错误提示failed to open stream: HTTP request failed!的完美解决方法
2011/06/06 PHP
浅谈php和js中json的编码和解码
2016/10/24 PHP
使用Yii2实现主从数据库设置
2016/11/20 PHP
Laravel框架中Blade模板的用法示例
2017/08/30 PHP
laravel请求参数校验方法
2019/10/10 PHP
用js生产批量批处理执行命令
2008/07/28 Javascript
XENON基于JSON变种
2010/07/27 Javascript
JavaScript之Object类型介绍
2015/04/01 Javascript
jQuery实现的多滑动门,多选项卡效果代码
2016/03/28 Javascript
Node.js读取文件内容示例
2017/03/07 Javascript
详解webpack+angular2开发环境搭建
2017/06/28 Javascript
基于jquery的on和click的区别详解
2018/01/15 jQuery
详解Vue中watch的高级用法
2018/05/02 Javascript
Vue press 支持图片放大功能的实例代码
2018/11/09 Javascript
d3绘制基本的柱形图的实现代码
2018/12/12 Javascript
使用vue-cli3+typescript的项目模板创建工程的教程
2020/02/28 Javascript
[40:31]Secret vs Alliacne 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
Python常见文件操作的函数示例代码
2011/11/15 Python
Python首次安装后运行报错(0xc000007b)的解决方法
2016/10/18 Python
Python中如何获取类属性的列表
2016/12/26 Python
Python实现 多进程导入CSV数据到 MySQL
2017/02/26 Python
Python paramiko模块的使用示例
2018/04/11 Python
PyTorch学习:动态图和静态图的例子
2020/01/06 Python
python 获取谷歌浏览器保存的密码
2021/01/06 Python
说出ArrayList,Vector, LinkedList的存储性能和特性
2015/01/04 面试题
应届生船舶驾驶求职信
2013/10/19 职场文书
前台接待员岗位职责
2014/01/02 职场文书
记帐员岗位责任制
2014/02/08 职场文书
教师对学生的寄语
2014/04/03 职场文书
2014国庆65周年领导讲话稿(3篇)
2014/09/21 职场文书
法定代表人身份证明书(含说明)
2014/10/02 职场文书
领导干部作风整顿剖析材料
2014/10/11 职场文书
小学生毕业评语
2014/12/26 职场文书
pytorch 权重weight 与 梯度grad 可视化操作
2021/06/05 Python
Mysql的Table doesn't exist问题及解决
2022/12/24 MySQL