简体   繁体   English

输入内容后jQuery UI自动完成

[英]jQuery UI autocomplete after input something

I want to autocomplete user name after input '@'(at sign). 我要在输入“ @”(符号后)后自动填写用户名。

form_html.erb: 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>

this is my coffeescript: 这是我的咖啡稿:

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

current appearance: 当前外观:

在此处输入图片说明

I want to input the "@"(at sign) then auto-complete user name. 我想输入“ @”(符号),然后自动填写用户名。

Please tell me if you have good ideas, Thanks in advance! 请告诉我您是否有好主意,谢谢!

根据这篇文章,您可以通过自定义$.ui.autocomplete.filter函数来完成此工作。

As @Bas van Stein suggestion, I can add @ sign to each name in json: 正如@Bas van Stein的建议,我可以在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

end 结束

then appearance: 然后外观:

在此处输入图片说明

It can work, but inefficient, not perfect. 它可以工作,但是效率低下,并不完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM