4
The element that looks like a <input>
is actually a <div>
, the <input>
this inside it and the tags that appear are Divs inserted with document.createElement
. Autocomplete (when typing) is really a matter of using ajax and interacting with the server.
However the problem seems to me only with the front-end even so there are plugins ready that can help you.
jQuery
For jQuery there are several plugins, but I will quote the https://github.com/xoxco/jQuery-Tags-Input:
Add this to the page:
<script src="jquery.tagsinput.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.tagsinput.css" />
Create an element like this:
<input name="tags" id="tags" value="foo,bar,baz" />
Put in the $.ready
or $(...)
this:
$(function() {
$('#tags').tagsInput();
})
It supports server interaction like this:
$('#tags').tagsInput({
autocomplete_url:'http://myserver.com/api/autocomplete'
});
Basic example:
$(function() {
$('#tag1').tagsInput({});
});
<link href="http://xoxco.com/examples/jquery.tagsinput.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://xoxco.com/examples/jquery.tagsinput.js"></script>
<div id="wrapper">
<p>Exemplo básico:
<input id="tag1" value="stack,overflow,portugues,olá,mundo" />
</p>
</div>
Angular.js
For angular there is the http://mbenford.github.io/ngTagsInput/:
Add after the angular.js:
<script type="text/javascript" src="path/to/angular.min.js"></script>
<script type="text/javascript" src="path/to/ng-tags-input.min.js"></script>
In the header add:
<link rel="stylesheet" href="path/to/ng-tags-input.min.css">
Add the application:
angular.module('myApp', ['ngTagsInput']);
To use:
<tags-input ng-model="tags"></tags-input>
You need the search system, even if only with javascript, or you just want the functionality of adding tags to the left of the text?
– Samir Braga
Just want the visual part, the logical part of the search would be too complex to ask here without at least having a ready base, so only the visual part of adding the tags to the left and highlighted would already be of good size.
– Renan Cavalieri