事件处理类
js 代码
- //控制器类
- TextSuggestKeyHandler = Class.create();
- TextSuggestKeyHandler.prototype = {
- //构造方法
- initialize: function( textSuggest,delay ) {
- //TextSuggest类的引用
- this.textSuggest = textSuggest;
- //输入框的引用
- this.input = this.textSuggest.textInput;
- this.delay = delay || 0.3;
- this.timer = null;
- this.lastValue = this.input.value;
- //为输入框增加事件响应机制
- this.addKeyHandling();
- },
- addKeyHandling: function() {
- this.input.onkeyup = this.keyupHandler.bindAsEventListener(this);
- this.input.onkeydown = this.keydownHandler.bindAsEventListener(this);
- this.input.onblur = this.onblurHandler.bindAsEventListener(this);
- if ( this.textSuggest.isOpera )
- this.input.onkeypress = this.keyupHandler.bindAsEventListener(this);
- },
- //按键按下事件响应
- keydownHandler: function(e) {
- var upArrow = 38;
- var downArrow = 40;
- if ( e.keyCode == upArrow ) {
- this.textSuggest.moveSelectionUp();
- //匹配选中在IE下会报selectRange()方法中的this.suggestions为空或不是对象 现也不需这功能暂时不用
- //setTimeout( this.selectRange.bind(this), 1 );
- }
- else if ( e.keyCode == downArrow ){
- this.textSuggest.moveSelectionDown();
- }
- },
- //放开按键事件响应
- keyupHandler: function(e) {
- if ( this.input.length == 0 && !this.isOpera )
- this.textSuggest.hideSuggestions();
- if ( !this.handledSpecialKeys(e) ){
- if(this.lastValue == this.input.value){
- return;
- }
- if(this.timer){
- clearTimeout(this.timer);
- }
- this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000);
- this.lastValue = this.input.value;
- }
- },
- onTimerEvent: function(){
- this.timer = null;
- this.textSuggest.handleTextInput();
- },
- //处理特殊按键
- handledSpecialKeys: function(e) {
- var iKeyCode = e.keyCode;
- var enterKey = 13;
- var upArrow = 38;
- var downArrow = 40;
- if ( iKeyCode == upArrow ||iKeyCode == downArrow ) {
- return true;
- }else if ( iKeyCode == enterKey ) {
- //回车则set入数据
- this.textSuggest.setInputFromSelection();
- return true;
- }
- if ((iKeyCode!=8 && iKeyCode < 32) || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)){
- return true;
- }
- return false;
- },
- //匹配选中
- selectRange: function() {
- var suggestion = this.suggestions[ this.selectedIndex ].districtName;
- var iStart = this.input.value.length;
- var iEnd = suggestion.length;
- //check for support of typeahead functionality
- if (this.input.createTextRange){
- this.input.value = suggestion;
- var oRange = this.input.createTextRange();
- oRange.moveStart("character", iStart);
- oRange.moveEnd("character", suggestion.length- this.input.value.length);
- oRange.select();
- //use setSelectionRange() for Mozilla
- } else if (this.input.setSelectionRange) {
- this.input.setSelectionRange(iStart, iEnd);
- }
- //set focus back to the textInput
- this.input.focus();
- },
- //失去焦点事件响应
- onblurHandler: function(e) {
- if ( this.textSuggest.suggestionsDiv.style.display == '' )
- //如果当前输入是显示的,那么点击其他地方应该把选择值注入输入框
- this.textSuggest.setInputFromSelection();
- this.textSuggest.hideSuggestions();
- }
- };
使用freemarker封装,使其成为组件,在需要的地方能方便的使用。autocomplete.ftl如下:通过dwrmethod参数将要使用的service方法传入前端处理程序。一并传入的还有显示样式
js 代码
- <#include "/${parameters.templateDir}/simple/text.ftl" />
- <script type="text/javascript">
- var suggestOptions = {
- //层样式
- suggestDivClassName: ${parameters.suggestDivClassName ? default("'suggestDiv'")},
- //选项样式
- suggestionClassName: ${parameters.suggestionClassName ? default("'suggestion'")},
- //匹配样式
- matchClassName : ${parameters.matchClassName ? default("'match'")},
- //是否匹配输入框宽度
- matchTextWidth : ${parameters.matchTextWidth ? default('true')},
- //选项颜色
- selectionColor : ${parameters.selectionColor ? default("'#FFB55E'")},
- //是否从头匹配
- matchAnywhere : ${parameters.matchAnywhere ? default('false')} ,
- //是否忽略大小写
- ignoreCase : ${parameters.ignoreCase ? default('false')},
- //显示数目
- count : ${parameters.count ? default(10)},
- //隐藏字段的Id
- hiddenId : ${parameters.hiddenId ? default('')}
- };
- new TextSuggest('${parameters.id}',${parameters.dwrMethod}, suggestOptions);
- </script>