Leave the textarea autocomplete when it increases in size

Asked

Viewed 150 times

1

I need to make the autocomplete track the textarea when it is augmented.

<form class="form-control form-inline">
     <label for="tags" class="control-label" id="upBody">Component: </label>
     <textarea id="tags" ng-model="components"
         class="search ng-pristine ng-touched ui-widget"
         placeholder="Insert a component" autocomplete="on">
     </textarea>
     <button ng-click="search(components)"
         class="btn btn-primary">See objects
     </button>
</form>

.autocomplete({
    appendTo: "#upBody",
    minLength: 0,
    source: function( request, response ) {
      response( $.ui.autocomplete.filter(
        availableTags, extractLast( request.term ) ) );
    },
    focus: function() {
      return false;
    },
    select: function( event, ui ) {
      var terms = split( this.value );
      terms.pop();
      terms.push( ui.item.value );
      terms.push( "" );
      this.value = terms.join( ", " );
      return false;
    }
  });

.ui-autocomplete {
     position: relative;
     overflow-y: auto;
     overflow-x: hidden;
     max-height: 150px;
 }

1 answer

0


You can add a event handler mousemove moving to top/bass the list element according to the size of the textarea, and at the same time adjusting the width:

$("#tags").on("mousemove", function(){
   $("#ui-id-1").css({
      "top": $(this).outerHeight()+$(this).offset().top +"px",
      "width": $(this).outerWidth() +"px"
   });
});

See example:

var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
$("#tags")
.on( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).autocomplete( "instance" ).menu.active ) {
          event.preventDefault();
        }
      })
.autocomplete({
   appendTo: "#upBody",
    minLength: 0,
    source: function( request, response ) {
      response( $.ui.autocomplete.filter(
        availableTags, extractLast( request.term )) );
    },
    focus: function() {
      return false;
    },
    select: function( event, ui ) {
      var terms = split( this.value );
      terms.pop();
      terms.push( ui.item.value );
      terms.push( "" );
      this.value = terms.join( ", " );
      return false;
    }
  });

function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }

$("#tags").on("mousemove", function(){
   $("#ui-id-1").css({
      "top": $(this).outerHeight()+$(this).offset().top +"px",
      "width": $(this).outerWidth() +"px"
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<form class="form-control form-inline">
     <label for="tags" class="control-label" id="upBody">Component: </label>
     <textarea id="tags" ng-model="components" class="search ng-pristine ng-touched ui-widget" placeholder="Insert a component" autocomplete="on"></textarea>
     <button ng-click="search(components)"
         class="btn btn-primary">See objects
     </button>
</form>

  • Thanks for the tip, @dvd. But in my case, I need that when I stretch the textarea, the autocomplete accompanies. Type, type part of a word, the autocomplete appears. When you stretch the textarea, autocomplete should follow.

  • This stretch that I speak is not when typing, but when maximizing the textarea, with the drag on the right, understand?

  • Example: The guy types a word and waits for the automplete to appear, when it appears the guy maximizes the textarea with drag and the autocomplete should go together.

  • @Antoniobrazfinizola Ah tah. Put the full code in the question so that it is possible to reproduce. I have to reproduce only as an example and I see that it is not the same as yours.

  • But my code is the same as the one you put there. I took it from the same place, jquery-ui. The only thing that changes is the data in the array.

  • Yes, @dvd. Thank you! Now I have another question involving the autocomplete. I just want to, for example, inform 3 of the autocomplete values. textfield will only keep this 3 values informed and will not allow typing more?

  • @Wouldn’t it be better to ask another specific question? Then other colleagues can also try to answer.

  • @Antoniobrazfinizola But if it’s too much work, I can see here for you.

  • You can leave it, @dvd. I’ll ask another question. Vlws.

  • Here’s the question, @dvd. Thanks in advance. https://answall.com/questions/273113/determinar-quantidade-de-itens-do-autocomplete-que-um-textfield-pode-permitir.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.