Dwr轻松实现google的suggest功能

2024-02-16 01:18

本文主要是介绍Dwr轻松实现google的suggest功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天将我正在进行开发的系统中注入了ajax的suggest功能,虽然不像我看的框架那样通用,但我感觉即简单又好用,我是将dwr框架引入程序中,在javascript中调用类方法查询到oracle数据库的数据,dwr真是快捷方便。下面是实现代码:
以下是SuggestControl.js
[code]
var arrOptions = new Array();
var strLastValue = "";
var bMadeRequest;
var theTextBox;
var objLastActive;
var currentValueSelected = -1;
var bNoResults = false;
var isTiming = false;
window.onload = function() {
var elemSpan = document.createElement("span");
elemSpan.id = "spanOutput";
elemSpan.className = "spanTextDropdown";
document.body.appendChild(elemSpan);
document.forms[0].consignOrgName.obj =
SetProperties(document.forms[0].consignOrgName,
document.forms[0].consignOrgValue,'',true,true,true,true,
"没有匹配的名称",false,true);
}
function SetProperties(xElem, xHidden, xserverCode, xignoreCase, xmatchAnywhere,
xmatchTextBoxWidth, xshowNoMatchMessage, xnoMatchingDataMessage, xuseTimeout,
xtheVisibleTime) {
var props = {
elem: xElem,
hidden: xHidden,
serverCode: xserverCode,
regExFlags: ((xignoreCase) ? "i" : ""),
regExAny: ((xmatchAnywhere) ? "^" : ""),
matchAnywhere: xmatchAnywhere,
matchTextBoxWidth: xmatchTextBoxWidth,
theVisibleTime: xtheVisibleTime,
showNoMatchMessage: xshowNoMatchMessage,
noMatchingDataMessage: xnoMatchingDataMessage,
useTimeout: xuseTimeout
};
AddHandler(xElem);
return props;
}
var isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
function AddHandler(objText){
objText.onkeyup = GiveOptions;
objText.onblur = function(){
if(this.obj.useTimeout)StartTimeout();
}
if(isOpera)objText.onkeypress = GiveOptions;
}
function GiveOptions(e){
var inkey = -1;
if(window.event){
inkey = event.keyCode;
theTextBox = event.srcElement;
}
else{
inkey = e.which;
theTextBox = e.target;
}
if(theTextBox.obj.useTimeout){
if(isTiming)EraseTimeout();
StartTimeout();
}
if(theTextBox.value.length == 0 && !isOpera){
arrOptions = new Array();
HideTheBox();
strLastValue = "";
return false;
}
if(objLastActive == theTextBox){
if(inkey == 13){
GrabHighlighted();
theTextBox.blur();
return false;
}
else if(inkey == 38){
MoveHighlight(-1);
return false;
}
else if(inkey == 40){
MoveHighlight(1);
return false;
}
}
if(objLastActive != theTextBox ||
theTextBox.value.
indexOf(strLastValue) != 0 ||
((arrOptions.length == 0 ||
arrOptions.length == 15)
&& !bNoResults) ||
(theTextBox.value.length
<= strLastValue.length)){

objLastActive = theTextBox;
bMadeRequest = true;
TypeAhead(theTextBox.value);
}
else if(!bMadeRequest){
BuildList(theTextBox.value);
}
strLastValue = theTextBox.value;
}
function TypeAhead(xStrText) {
arrOptions = [];
GetData.getData(xStrText, BuildList);
bMadeRequest = false;
}
function CreateArray(ajaxResponse){
for(var index in ajaxResponse){

var newtext = ajaxResponse[index].fcname;
var newval = ajaxResponse[index].fcode;
arrOptions.push(new Array(newval,newtext));
}
}
function BuildList(theText){
CreateArray(theText);
SetElementPosition(theTextBox);

var theMatches = MakeMatches(strLastValue);
theMatches = theMatches.join().replace(/\,/gi,"");
if(theMatches.length > 0){
document.getElementById("spanOutput").innerHTML = theMatches;
document.getElementById("OptionsList_0").className = "spanHighElement";
currentValueSelected = 0;
bNoResults = false;
}
else{
currentValueSelected = -1;
bNoResults = true;
if(theTextBox.obj.showNoMatchMessage)
document.getElementById("spanOutput").innerHTML =
"<span class='noMatchData'>" + theTextBox.obj.noMatchingDataMessage + "</span>";
else
HideTheBox();
}
}
function SetElementPosition(theTextBoxInt){
var selectedPosX = 0;
var selectedPosY = 0;
var theElement = theTextBoxInt;
if(!theElement) return;
var theElemHeight = theElement.offsetHeight;
var theElemWidth = theElement.offsetWidth;
while(theElement != null){
selectedPosX += theElement.offsetLeft;
selectedPosY += theElement.offsetTop;
theElement = theElement.offsetParent;
}
xPosElement = document.getElementById("spanOutput");
xPosElement.style.left = selectedPosX;
if(theTextBoxInt.obj.matchTextBoxWidth)
xPosElement.style.width = theElemWidth;
xPosElement.style.top = selectedPosY + theElemHeight;
xPosElement.style.display = "block";
if(theTextBoxInt.obj.useTimeout){
xPosElement.onmouseout = StartTimeout;
xPosElement.onmouseover = EraseTimeout;
}
else{
xPosElement.onmouseout = null;
xPosElement.onmouseover = null;
}
}
var countForId = 0;
function MakeMatches(xCompareStr){
countForId = 0;
var theMatch;
var matchArray = new Array();
var regExp = new RegExp(theTextBox.obj.regExAny +
xCompareStr,theTextBox.obj.regExFlags);
for(i = 0; i < arrOptions.length; i++){
if(arrOptions[i][1] != null)
theMatch = arrOptions[i][1].match(regExp);
if(theMatch){
matchArray[matchArray.length]=
CreateUnderline(arrOptions[i][1],xCompareStr,i);
}
}
return matchArray;
}
function CreateUnderline(xStr,xTextMatch,xVal){
var undeStart = "<span class='spanMatchText'>";
var undeEnd = "</span>";
var aStart;
var matchedText;
var xreplace='';
var selectSpanStart = "<span style='width:100%;display:block;'class='spanNormalElement'
οnmοuseοver='SetHighColor(this)'";
var selectSpanEnd = "</span>";
selectSpanMid = "οnclick='SetText("+xVal+")'" +"id='OptionsList_" + countForId +
"' theArrayNumber='"+xVal+"'>";
var regExp = new RegExp(theTextBox.obj.regExAny +
xTextMatch,theTextBox.obj.regExFlags);
if(xStr != undefined){
aStart = xStr.search(regExp);
matchedText = xStr.substring(aStart,aStart + xTextMatch.length);
xreplace = xStr.replace(regExp,undeStart + matchedText + undeEnd);
countForId++;
}
return selectSpanStart + selectSpanMid + xreplace + selectSpanEnd;

}
function MoveHighlight(xDir){
if(currentValueSelected >= 0){
newValue = parseInt(currentValueSelected) + parseInt(xDir);
if(newValue > -1 && newValue < countForId){
currentValueSelected = newValue;
SetHighColor(null);
}
}
}
function SetHighColor(theTextBox){
if(theTextBox){
currentValueSelected =
theTextBox.id.slice(theTextBox.id.indexOf("_") + 1,
theTextBox.id.length);
}
for(i = 0; i < countForId; i ++){
document.getElementById('OptionsList_'+i).className =
'spanNormalElement';
}
document.getElementById('OptionsList_' +
currentValueSelected).className = 'spanHighElement';
}
function SetText(xVal){
theTextBox.value = arrOptions[xVal][1];
theTextBox.obj.hidden.value = arrOptions[xVal][0];
document.getElementById("spanOutput").style.display = "none";
currentValueSelected = -1;
}
function GrabHighlighted(){
if(currentValueSelected >= 0){
xVal = document.getElementById("OptionsList_" +
currentValueSelected).getAttribute("theArrayNumber");
SetText(xVal);
HideTheBox();
}
}
function HideTheBox(){
document.getElementById("spanOutput").style.display = "none";
currentValueSelected = -1;
EraseTimeout();
}
function EraseTimeout(){
clearTimeout(isTiming);
isTiming = false;
}
function StartTimeout(){
isTiming = setTimeout("HideTheBox()",theTextBox.obj.theVisiableTime);
}
[/code]
以下是class GetDate 及类方法,以获得要查询的数据
[code]
public GetData(){}

public List getData(String str){
List list = null;
Client cl = null;
try {
con = ConnectionHandler.getConnection("seaDataSource");
st = con.createStatement();
rs = st.executeQuery("select F_CODE,F_CNAME from view_collect where " +
"F_SHORT like '"+str+"%' and rownum <= 5");
while(rs.next()) {
cl = new Client();
cl.setFcode(rs.getString("F_CODE"));
cl.setFcname(rs.getString("F_CNAME"));
// System.out.println("---"+cl.getFcname());
if(list==null)list = new ArrayList();
list.add(cl);
cl = null;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
rs.close();
st.close();
con.close();
} catch (Exception e)
{e.printStackTrace();}
}
return list;
}
[/code]
以下是dwr.xml中的配置
[code]
<dwr>
<allow>
<create creator="new" javascript="GetData">
<param name="class" value="com.fmslite.seaexp.dao.GetData" />
</create>
<convert converter="bean" match="com.fmslite.seaexp.pojo.Client"/>
</allow>
</dwr>
//*********************以下是web.xml中的配置********************************//
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
[/code]
以下是css的sheet文件
[code]
.span.spanTextDropdown {
position: absolute;
top: 0px;
left: 0px;
width: 150px;
z-index: 101;
background-color: #F6EEFF;
border: 1px solid #8E8DAA;
padding-left: 2px;
overflow: visible;
display: none;
font-size:8pt
}
.span.spanMatchText {
color:#B8B8FF;
font-weight: bold;
font-size:8pt
}
.span.spanNormalElement {
background: #F6EEFF;
font-size:8pt
}
.span.spanHighElement {
font-size:8pt;
background: #A2A1C5;
color: #FFFFFF;
cursor: pointer
}
.span.noMatchData {
font-weight: bold;
color: #0000ff;
font-size:8pt
}
[/code]
如能配置正确以上文件及路径,即可轻松实现Suggest的基本功能。

这篇关于Dwr轻松实现google的suggest功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/713093

相关文章

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

使用Python实现一个简易计算器的新手指南

《使用Python实现一个简易计算器的新手指南》计算器是编程入门的经典项目,它涵盖了变量、输入输出、条件判断等核心编程概念,通过这个小项目,可以快速掌握Python的基础语法,并为后续更复杂的项目打下... 目录准备工作基础概念解析分步实现计算器第一步:获取用户输入第二步:实现基本运算第三步:显示计算结果进

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

Python利用PySpark和Kafka实现流处理引擎构建指南

《Python利用PySpark和Kafka实现流处理引擎构建指南》本文将深入解剖基于Python的实时处理黄金组合:Kafka(分布式消息队列)与PySpark(分布式计算引擎)的化学反应,并构建一... 目录引言:数据洪流时代的生存法则第一章 Kafka:数据世界的中央神经系统消息引擎核心设计哲学高吞吐

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图