Javascript噩梦-Ajax实现输入提示的调整与配置

经过一个星期的煎熬,终于把基于Ajax的输入提示功能实现了。太痛苦了,写Javascript的感觉就跟用NotePad来写代码一样,没有智能提示、弱类型、难调试……总之是太折磨人了。
本来自己写了一个比较简单的,但是由于我的页面上需要多个输入框,还要可以动态增加输入框,要把传回来的结果set入多个输入框,由于是使用的Struts标签库,<html:text>还没有id属性,让这个问题复杂了不少。
需求是这样的:
有一个页面,需要录入货品信息,货品有id,编号,名称,单位,单价,规格等属性,每个货品信息在表格中有一行,一行中有多个输入框,用于输入货品信息。在输入货品编号的时候,应该访问后台的商品信息库,寻找以前是否有输入过该编号的货品,如果有,把编号返回。支持用鼠标点击,键盘回车等方法选择货品,选择后应该把货品信息显示到各个输入框中供用户修改。如果该货品在商品信息库中标记为敏感商品,要作出提示。一个编号可以有多个货品。
改了3天代码,终于决定破釜沉舟,删掉重做。修改了《Ajax in Action》中的代码,Ajax in Action中的代码有些地方有错误,不仔细检查一遍还真不太容易发现。书中后台使用C#,前台是使用Rico来向某个url传参数的形式进行Ajax通信。返回的response类似:
None.gif < ajax-response >
None.gif  
< response  type ='object'  id ='field1_updater'>
None.gif    
<matches >
None.gif      
< text > XXX </ text >
None.gif      
< value > XXX </ value >
None.gif    
</ matches >
None.gif  
</ response >
None.gif
</ ajax-response >
不过我不想写JSP或者Servlet,所以用了DWR,直接用spring中的BO把结果传回来:
ExpandedBlockStart.gif ContractedBlock.gif cargobaseService.getByNumberAndCompany( this .lastRequestString, this .company, function (ajaxResponse) dot.gif {
InBlock.gif
//dot.gif
ExpandedBlockEnd.gif
}
);
cargobaseService是使用DWR创建的Javascript对象:
dwr.xml:
None.gif < dwr >
None.gif  
< allow >
None.gif    
< create  creator ="spring"  javascript  = "cargobaseService" >
None.gif        
< param  name ="beanName"  value ="cargobaseService" />
None.gif    
</ create >
None.gif    
< convert  match ="com.gdnfha.atcs.cargobase.model.*"  converter ="bean" ></ convert >
None.gif  
</ allow >
None.gif
</ dwr >
None.gif
返回为下面对象的数组
ExpandedBlockStart.gif ContractedBlock.gif var  cargoModel  =   dot.gif {
InBlock.gif  cargoCompany: XXX,
InBlock.gif  cargoCurrency: XXX,
InBlock.gif  cargoDestination: XXX,
InBlock.gif  cargoId: XXX,
InBlock.gif  cargoImpose: XXX,
InBlock.gif  cargoName: XXX,
InBlock.gif  cargoNumber: XXX,
InBlock.gif  cargoSize: XXX,
InBlock.gif  cargoUnit: XXX,
InBlock.gif  cargoUnitPrice: XXX,
InBlock.gif  sensitive: 
true|false
ExpandedBlockEnd.gif}
 Javascript代码如下:
None.gif TextSuggest  =  Class.create();
None.gif
ExpandedBlockStart.gifContractedBlock.gifTextSuggest.prototype 
=   dot.gif {
InBlock.gif    
//构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
   initialize: function(anId,company, url, options) dot.gif{
InBlock.gif      
this.id          = anId;
InBlock.gif      
this.company = company;
InBlock.gif      
var browser = navigator.userAgent.toLowerCase();
InBlock.gif      
this.isIE        = browser.indexOf("msie"!= -1;
InBlock.gif      
this.isOpera     = browser.indexOf("opera")!= -1;
InBlock.gif      
this.textInput   = $(this.id);
InBlock.gif      
this.suggestions = new Array();
InBlock.gif      
this.setOptions(options);
InBlock.gif      
this.injectSuggestBehavior();
ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//设置参数
ExpandedSubBlockStart.gifContractedSubBlock.gif
   setOptions: function(options) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
this.options = dot.gif{
InBlock.gif         suggestDivClassName: 'suggestDiv',
InBlock.gif         suggestionClassName: 'suggestion',
InBlock.gif         matchClassName     : 'match',
InBlock.gif         matchTextWidth     : 
true,
InBlock.gif         selectionColor     : '#b1c09c',
InBlock.gif         matchAnywhere      : 
false,
InBlock.gif         ignoreCase         : 
false,
InBlock.gif         count              : 
10
ExpandedSubBlockStart.gifContractedSubBlock.gif      }
.extend(options || dot.gif{});
ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//注入输入提示行为
ExpandedSubBlockStart.gifContractedSubBlock.gif
   injectSuggestBehavior: function() dot.gif{
InBlock.gif
InBlock.gif      
if ( this.isIE )
InBlock.gif         
this.textInput.autocomplete = "off";
InBlock.gif    
//创建控制器
InBlock.gif
      var keyEventHandler = new TextSuggestKeyHandler(this);
InBlock.gif      
//主要是为了避免在按回车的时候把表单提交
InBlock.gif
      new Insertion.After( this.textInput,
InBlock.gif                           '
<input type="text" id="'+this.id+'_preventtsubmit'+'" style="display:none"/>' );
InBlock.gif      
new Insertion.After( this.textInput,
InBlock.gif                           '
<input type="hidden" name="'+this.id+'_hidden'+'" id="'+this.id+'_hidden'+'"/>' );
InBlock.gif    
//创建div层
InBlock.gif
      this.createSuggestionsDiv();
ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//处理输入信息
ExpandedSubBlockStart.gifContractedSubBlock.gif
   handleTextInput: function() dot.gif{
InBlock.gif     
var previousRequest    = this.lastRequestString;
InBlock.gif     
this.lastRequestString = this.textInput.value;
InBlock.gif     
if ( this.lastRequestString == "" )
InBlock.gif        
this.hideSuggestions();
ExpandedSubBlockStart.gifContractedSubBlock.gif     
else if ( this.lastRequestString != previousRequest ) dot.gif{
InBlock.gif         
//访问数据源
InBlock.gif
        this.sendRequestForSuggestions();
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//选择框上移
ExpandedSubBlockStart.gifContractedSubBlock.gif
   moveSelectionUp: function() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if ( this.selectedIndex > 0 ) dot.gif{
InBlock.gif         
this.updateSelection(this.selectedIndex - 1);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//选择框下移
ExpandedSubBlockStart.gifContractedSubBlock.gif
   moveSelectionDown: function() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if ( this.selectedIndex < (this.suggestions.length - 1)  ) dot.gif{
InBlock.gif         
this.updateSelection(this.selectedIndex + 1);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//更新当前选择信息
ExpandedSubBlockStart.gifContractedSubBlock.gif
   updateSelection: function(n) dot.gif{
InBlock.gif      
var span = $( this.id + "_" + this.selectedIndex );
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if ( span )dot.gif{
InBlock.gif          
//消除以前的样式
InBlock.gif
         span.style.backgroundColor = "";
ExpandedSubBlockEnd.gif      }

InBlock.gif      
this.selectedIndex = n;
InBlock.gif      
var span = $( this.id + "_" + this.selectedIndex );
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if ( span )dot.gif{
InBlock.gif          
//更新新样式
InBlock.gif
         span.style.backgroundColor = this.options.selectionColor;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }
,
InBlock.gif    
//发送请求
ExpandedSubBlockStart.gifContractedSubBlock.gif
   sendRequestForSuggestions: function() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
if ( this.handlingRequest ) dot.gif{
InBlock.gif        
this.pendingRequest = true;
InBlock.gif        
return;
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     
this.handlingRequest = true;
InBlock.gif     
this.callDWRAjaxEngine();
ExpandedSubBlockEnd.gif   }
,
InBlock.gif
InBlock.gif    
//使用DWR访问后台
ExpandedSubBlockStart.gifContractedSubBlock.gif
   callDWRAjaxEngine: function() dot.gif{
InBlock.gif           
//保存当前对象指针
InBlock.gif
           var tempThis = this;
ExpandedSubBlockStart.gifContractedSubBlock.gif           cargobaseService.getByNumberAndCompany(
this.lastRequestString,this.company,function(ajaxResponse)dot.gif{
InBlock.gif            tempThis.suggestions 
= ajaxResponse;
ExpandedSubBlockStart.gifContractedSubBlock.gif              
if ( tempThis.suggestions.length == 0 ) dot.gif{
InBlock.gif                 tempThis.hideSuggestions();
InBlock.gif                 $( tempThis.id 
+ "_hidden" ).value = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif              }
else dot.gif{
InBlock.gif                 tempThis.updateSuggestionsDiv();
InBlock.gif                 tempThis.showSuggestions();
InBlock.gif                 tempThis.updateSelection(
0);
h
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值