2015-09-22 22 views
0

我想在輸入'@'(在符號)後自動完成用戶名。輸入東西后jQuery UI自動完成

form_html.erb:

<div class="form-group ui-widget"> 
    <div class="col-sm-5"> 
     <%= f.text_area :content, rows: 5, placeholder: '...', class: 'form-control', id: "tags", 'data-article-id': @article.id.to_s %> 
    </div> 
    </div> 

這是我的CoffeeScript:

$ -> 
    id = $("#tags").data("article-id") 
    $("#tags").autocomplete 
    source: '/articles/' + id + '/autocomplete.json' 
    minLength: 1 

當前外觀:

enter image description here

我想輸入 「@」(at符號)然後自動完成用戶名。

請告訴我,如果你有好的想法,請提前致謝!

+0

剛剛初步考慮了關鍵點檢查textarea的長度;如果長度= 1;用用戶輸入的值預先輸入@sign ... – Akki619

+1

您也可以在@符號中爲json中的每個名稱。 –

+0

@BasvanStein你給我很好的主意,謝謝! – pangpang

回答

0

由於@Bas麪包車斯坦因的建議,我可以@符號添加到每個名稱以JSON:

def autocomplete 
    @articles = Article.find_by(id: params[:article_id]) 
    @commentor_names = @articles.comments.map{|e| "@" + e.name}.uniq 
             .map{|e| e if e.match(/^#{params[:term]}/i) }.compact 
    respond_to do |format| 
     format.html 
     format.json { 
     render json: @commentor_names 
     } 
    end 

然後外觀:

enter image description here

它可以工作,但效率低下,不完美。

0

根據this post您可以通過自定義$.ui.autocomplete.filter函數來完成這項工作。