2016-06-07 116 views
2

我想了解jquery提及插件jquery mentions,我在插件中遇到了这个部分。任何人都可以解释这是什么意思?最后的返回函数特别是做了什么? 我想关闭自动完成下拉当匹配值具有长度小于4jquery提到jquery ui自动完成

 search: function (value, event) { 
     var match, pos; 
     //&& value.length >= this.options.minChars 
     if (!value) { 
      //sel = window.getSelection(); 
      //node = sel.focusNode; 
      value = this._value(); 
      pos = Selection.get(this.element).start; 
      value = value.substring(0, pos); 
      match = this.matcher.exec(value); 
      if (!match || match[1].length <= this.options.minChars) { 
       return ''; 
      } 

      this.start = match.index; 
      this.end = match.index + match[0].length; 
      this.searchTerm = match[1]; 
      //this._setDropdownPosition(node); 
     } 
     return $.ui.autocomplete.prototype.search.call(this, this.searchTerm, event); 
    } 

回答

1

我解决了这个,因为我发现searchterm没有重置,下拉菜单没有返回空值。所以我改变了这样的搜索代码。

search: function (value, event) { 
     var match, pos; 
     //&& value.length >= this.options.minChars 
     if (!value) { 
      //sel = window.getSelection(); 
      //node = sel.focusNode; 
      value = this._value(); 
      pos = Selection.get(this.element).start; 
      value = value.substring(0, pos); 
      match = this.matcher.exec(value); 
      if (!match) { 

       return ''; 
      } 


      this.start = match.index; 
      this.end = match.index + match[0].length; 
      this.searchTerm = match[1]; 
      if (match[1].length <= this.options.minChars) {//customization: to check minChars 

       this.searchTerm = '';// customization: to clear autocomplete dropdown 
      } 
      this._setDropdownPosition($('.mentions')[0]); 
     } 
     return $.ui.autocomplete.prototype.search.call(this,this.searchTerm, event); 
    } 
0

在该插件,新jQueryUI的窗口小部件的定义 - ui.editablecompleteui.areacomplete。这两个小部件基本上扩展了常规的autocomplete小部件,以支持<textarea>元素和contenteditable元素。

只要您输入任何内容(<input type="text">,<textarea>等),搜索方法正在运行。如果它通过search方法中的所有if语句,它将处理数据并调用最后一个return语句:$.ui.autocomplete.prototype.search.call(this, this.searchTerm, event);基本上告知autocomplete小组件接管并像往常一样继续其所有操作。这可以类似于在经典继承中重写。

无论如何,如果您只想打开自动完成下拉菜单的时间超过4个字符,则需要更改matcher正则表达式。默认情况下,它是/\B[@]([^@]{0,20})$/,它将输入的长度限制在0到20个字符之间。我没有看到通过API改变它的方法,所以我想你需要稍微改变代码。

此功能定义了matcher

MentionsBase.prototype._getMatcher = function() { 
    var allowedChars; 
    allowedChars = '[^' + this.options.trigger + ']'; 
    return '\\B[' + this.options.trigger + '](' + allowedChars + '{0,20})'; 
}; 

(如果你想GT,而不是GTE或{5,20})您可以更改{0,20}{4,20}

一个更好的想法是创建一个向插件的作者提供一个将匹配正则表达式公开给API的pull请求,而不是更改代码。

+0

谢谢,但我解决了这个问题。 :) 现在我坚持将自动完成下拉菜单放在textarea中的光标处。我GOOGLE了并尝试了所有的解决方案,但没有运气。 – orangespark