ITeye博客

使用DWR实现搜索自动提示(三)

sprite · 2007-06-28 21:59 · 程序设计 · Markdown

事件处理类

js 代码

  1. //控制器类
  2. TextSuggestKeyHandler = Class.create();
  3. TextSuggestKeyHandler.prototype = {
  4. //构造方法
  5. initialize: function( textSuggest,delay ) {
  6. //TextSuggest类的引用
  7. this.textSuggest = textSuggest;
  8. //输入框的引用
  9. this.input       = this.textSuggest.textInput;
  10. this.delay     = delay || 0.3;
  11. this.timer     = null;
  12. this.lastValue = this.input.value;
  13. //为输入框增加事件响应机制
  14. this.addKeyHandling();
  15. },
  16. addKeyHandling: function() {
  17. this.input.onkeyup    = this.keyupHandler.bindAsEventListener(this);
  18. this.input.onkeydown  = this.keydownHandler.bindAsEventListener(this);
  19. this.input.onblur     = this.onblurHandler.bindAsEventListener(this);
  20. if ( this.textSuggest.isOpera )
  21. this.input.onkeypress = this.keyupHandler.bindAsEventListener(this);
  22. },
  23. //按键按下事件响应
  24. keydownHandler: function(e) {
  25. var upArrow   = 38;
  26. var downArrow = 40;
  27. if ( e.keyCode == upArrow ) {
  28. this.textSuggest.moveSelectionUp();
  29. //匹配选中在IE下会报selectRange()方法中的this.suggestions为空或不是对象 现也不需这功能暂时不用
  30. //setTimeout( this.selectRange.bind(this), 1 );
  31. }
  32. else if ( e.keyCode == downArrow ){
  33. this.textSuggest.moveSelectionDown();
  34. }
  35. },
  36. //放开按键事件响应
  37. keyupHandler: function(e) {
  38. if ( this.input.length == 0 && !this.isOpera )
  39. this.textSuggest.hideSuggestions();
  40. if ( !this.handledSpecialKeys(e) ){
  41. if(this.lastValue == this.input.value){
  42. return;
  43. }
  44. if(this.timer){
  45. clearTimeout(this.timer);
  46. }
  47. this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000);
  48. this.lastValue = this.input.value;
  49. }
  50. },
  51. onTimerEvent: function(){
  52. this.timer = null;
  53. this.textSuggest.handleTextInput();
  54. },
  55. //处理特殊按键
  56. handledSpecialKeys: function(e) {
  57. var iKeyCode  = e.keyCode;
  58. var enterKey  = 13;
  59. var upArrow   = 38;
  60. var downArrow = 40;
  61. if ( iKeyCode == upArrow ||iKeyCode == downArrow ) {
  62. return true;
  63. }else if ( iKeyCode == enterKey ) {
  64. //回车则set入数据
  65. this.textSuggest.setInputFromSelection();
  66. return true;
  67. }
  68. if ((iKeyCode!=8 && iKeyCode < 32) || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)){
  69. return true;
  70. }
  71. return false;
  72. },
  73. //匹配选中
  74. selectRange: function() {
  75. var suggestion  = this.suggestions[ this.selectedIndex ].districtName;
  76. var iStart = this.input.value.length;
  77. var iEnd   = suggestion.length;
  78. //check for support of typeahead functionality
  79. if (this.input.createTextRange){
  80. this.input.value = suggestion;
  81. var oRange = this.input.createTextRange();
  82. oRange.moveStart("character", iStart);
  83. oRange.moveEnd("character",  suggestion.length- this.input.value.length);
  84. oRange.select();
  85. //use setSelectionRange() for Mozilla
  86. } else if (this.input.setSelectionRange) {
  87. this.input.setSelectionRange(iStart, iEnd);
  88. }
  89. //set focus back to the textInput
  90. this.input.focus();
  91. },
  92. //失去焦点事件响应
  93. onblurHandler: function(e) {
  94. if ( this.textSuggest.suggestionsDiv.style.display == '' )
  95. //如果当前输入是显示的,那么点击其他地方应该把选择值注入输入框
  96. this.textSuggest.setInputFromSelection();
  97. this.textSuggest.hideSuggestions();
  98. }
  99. };

 

使用freemarker封装,使其成为组件,在需要的地方能方便的使用。autocomplete.ftl如下:通过dwrmethod参数将要使用的service方法传入前端处理程序。一并传入的还有显示样式

js 代码

  1. <#include "/${parameters.templateDir}/simple/text.ftl" />
  2. <script type="text/javascript">
  3. var suggestOptions = {
  4. //层样式
  5. suggestDivClassName: ${parameters.suggestDivClassName ? default("'suggestDiv'")},
  6. //选项样式
  7. suggestionClassName: ${parameters.suggestionClassName ? default("'suggestion'")},
  8. //匹配样式
  9. matchClassName     : ${parameters.matchClassName ? default("'match'")},
  10. //是否匹配输入框宽度
  11. matchTextWidth     : ${parameters.matchTextWidth ? default('true')},
  12. //选项颜色
  13. selectionColor     : ${parameters.selectionColor ? default("'#FFB55E'")},
  14. //是否从头匹配
  15. matchAnywhere      : ${parameters.matchAnywhere ? default('false')} ,
  16. //是否忽略大小写
  17. ignoreCase         : ${parameters.ignoreCase ? default('false')},
  18. //显示数目
  19. count              : ${parameters.count ? default(10)},
  20. //隐藏字段的Id
  21. hiddenId         : ${parameters.hiddenId ? default('')}
  22. };
  23. new TextSuggest('${parameters.id}',${parameters.dwrMethod}, suggestOptions);
  24. </script>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。