Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete tags with forcing and free text

I need to have a multiline textbox with a free text allowed, but If I start type-in characters: "@@"
the autocomplete box with available tags should show up and allow me to select tags from existing, and "@@" characters should not be present in the tag name.

Currently I'm playing with tag-it plugin for jquery UI but cannot figure out how to allow free text and only shw autocomplete box on "@@" enter. and how to user textarea instead of regular input.

Also, I'd like to force them to select from the list if they enter @@ and do not allow to type free text (first available selection would be good)
Javascript:

$(document).ready(function() {

  //The demo tag array
  var availableTags = [
    {value: 1, label: 'tag1'},
    {value: 2, label: 'tag2'},
    {value: 3, label: 'tag3'}];

  //The text input
  var input = $("input#text");

  //The tagit list
  var instance = $("<ul class=\"tags\"></ul>");

  //Store the current tags
  //Note: the tags here can be split by any of the trigger keys
  //      as tagit will split on the trigger keys anything passed  
  var currentTags = input.val();

  //Hide the input and append tagit to the dom
  input.hide().after(instance);

  //Initialize tagit
  instance.tagit({
    tagSource:availableTags,
    tagsChanged:function () {

      //Get the tags            
      var tags = instance.tagit('tags');
      var tagString = [];

      //Pull out only value
      for (var i in tags){
        tagString.push(tags[i].value);
      }

      //Put the tags into the input, joint by a ','
      input.val(tagString.join(','));
    }
  });

  //Add pre-loaded tags to tagit
  instance.tagit('add', currentTags);
});

html:

<p>This example shows how to use Tagit on an input!</p>
<input type="text" id="text" name="tags" value="one,two,three"/>
​

testing here: http://jsfiddle.net/hailwood/u8zj5/

like image 887
user194076 Avatar asked Nov 15 '12 02:11

user194076


People also ask

Why Autocomplete is not working?

Sometimes, the most basic answer is the best answer. One, autocomplete requires an internet connection to work, so make sure that your internet is working first and foremost. Apart from that, make sure to enable the autocomplete feature.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })

What is jQuery autocomplete?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is autocomplete in JavaScript?

The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try. The datasource is a simple JavaScript array, provided to the widget using the source-option.


1 Answers

Since you had used tag-it plugin.. I have added some handler to the input to handle

  1. @@ to show auto complete as you type
  2. Free text if typed without @@

I still need time to look into the Do not allow free text if @@ is typed

DEMO: http://jsfiddle.net/xBgfJ/2/ and below is the full code,

Note: Below code is tweak to the existing plugin code.

$(document).ready(function() {

    //The demo tag array
    var availableTags = [{value: 1, label: 'tag1'},{ value: 2,label: 'tag2'}, { value: 3, label: 'tag3'}];

    //The text input
    var input = $("input#text");

    //The tagit list
    var instance = $("<ul class=\"tags\"></ul>");

    //Store the current tags
    //Note: the tags here can be split by any of the trigger keys
    //      as tagit will split on the trigger keys anything passed  
    var currentTags = input.val();

    //Hide the input and append tagit to the dom
    input.hide().after(instance);

    //Initialize tagit
    instance.tagit({
        tagSource: availableTags,
        tagsChanged: function() {

            //Get the tags            
            var tags = instance.tagit('tags');
            var tagString = [];

            //Pull out only value
            for (var i in tags) {
                tagString.push(tags[i].value);
            }

            //Put the tags into the input, joint by a ','
            input.val(tagString.join(','));
        },
        onTagAdded: function() {
            inpNext.parent().find('.pre-filter').remove();
        }
    });

    //Add pre-loaded tags to tagit
    instance.tagit('add', currentTags);

    var inpNext = input.next();
    var autoCompelteMenu = $('.ui-autocomplete', inpNext);

    inpNext.on('keydown', '.tagit-input', function(e) {
        var $parent = $(this).parent();
        var $preFilter = $parent.find('.pre-filter');
        if (e.which == 8 && this.value == '') { //backspace           
            $preFilter.remove();
        } else if (e.which == 9 || e.which == 32
                  || e.which == 188 || e.which ==  44 ||
                  e.which == 13 ) { //tab or space, comma and enter
            $preFilter.remove();
            autoCompelteMenu.css('opacity', 0);
        }

    }).on('keypress', '.tagit-input', function(e) {

        var $parent = $(this).parent();
        var $preFilter = $parent.find('.pre-filter');

        if (e.which == 64 && !$preFilter.length) {
            $parent.prepend('<span class="pre-filter hidden">@</span>');
            autoCompelteMenu.css('opacity', 0);
        } else if ( e.which == 64 && $preFilter.length) {
            e.preventDefault();
            this.value = '';
            $preFilter.append('@').removeClass('hidden');
            autoCompelteMenu.css('opacity', 1);
        }

        return;

    }).on('blur', '.tagit-input', function() {
        $(this).parent().find('span').remove();
        autoCompelteMenu.css('opacity', 0);
    });
});
like image 106
Selvakumar Arumugam Avatar answered Oct 10 '22 18:10

Selvakumar Arumugam