Add jQuery to autocomplete data in textual content instead of overwriting it

I am trying to make @other people function in my rails application, just like stackoverflow:

enter image description here

I almost finished this function, but I ran into problems, the jQuery autocomputer data replaces the contents of my text area rather than add.

enter image description here

CoffeeScript:

find_at_sign = ->
  if $("#tags").val().split('').pop() == "@"
    id = $("#tags").data("article-id")
    $("#tags").autocomplete
      source:  '/articles/' + id + '/autocomplete.json'
      minLength: 1

$ ->
  $(document).on("input", "#tags",
  -> find_at_sign())

Article controller:

  def autocomplete
    @articles = Article.find_by(id: params[:article_id])
    @commentor_names = @articles.comments.map(&:name).uniq
    respond_to do |format|
      format.html
      format.json {
        render json: @commentor_names
      }
    end
  end

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>

I tried to use the method append, but it does not work:

$("#tags").append(${this).autocomplete
  source:  '/articles/' + id + '/autocomplete.json'
  minLength: 1)

Any help would be appreciated!

+4
source share
3 answers

, . :

var namelist = [
  "Adam",
  "Adrian",
  "Andrew",
  "Charles",
  "Daniel",
  "David",
  "Evan",
  "Henry",
  "Ian",
  "Jack",
  "James",
  "John",
  "Joseph",
  "Justin",
  "Kevin",
  "Michael",
  "Parker",
  "Robert",
  "Thomas",
  "William"
];
$(function() {
  $("#message").autocomplete({
    source: function(request, response) {
      var match = request.term.match(/@(\w*)$/);
      var names = match ? $.ui.autocomplete.filter(namelist, match[1]) : [];
      response(names)
    },
    focus: function(event) {
      event.preventDefault();
    },
    select: function(event, ui) {
      event.preventDefault();
      this.value = this.value.replace(/@(\w*)$/, "@" + ui.item.value)
    }
  })
});
@import url("http://code.jquery.com/ui/1.11.1/themes/ui-darkness/jquery-ui.min.css");

body {
  font: smaller sans-serif;
}
textarea {
  box-sizing: border-box;
  width: 100%;
  height: 5em;
}
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>


<p>Enter some text or @name</p>
<textarea id="message"></textarea>
+1

:

  • replace @ , .

  • , .

:

 function (tid) {

        node=$('#'+tid)[0];

        try{
        //--- Wrap selected text or insert at curser.
        var oldText         = node.value || node.textContent;
        var newText;
        var iTargetStart    = node.selectionStart;
        var iTargetEnd      = node.selectionEnd;

        if (iTargetStart == iTargetEnd)
            newText         = left+right;
        else
            newText         = left + oldText.slice (iTargetStart, iTargetEnd) + right;

        //console.log (newText);
        newText             = oldText.slice (0, iTargetStart) + newText + oldText.slice (iTargetEnd);
        node.value          = newText;
        //-- After using spelling corrector, this gets buggered, hence the multiple sets.
        node.textContent    = newText;
        //-- Have to reset selection, since we repasted the text.
        node.selectionStart = iTargetStart + left.length;
        node.selectionEnd   = iTargetEnd   + left.length;
            node.focus ();
....

.

0
source

Source: https://habr.com/ru/post/1609202/


All Articles