2016-06-07 131 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