This jQuery plugin providing a Material Design user interface for managing tags
Code on Github Materialize v0.100.2 TypeAhead v1.2.1 Download as ZIP
You can also get the latest release using NPM. This release contains source files as well as the compiled CSS and JavaScript files.
npm install materialize-tags
Just add data-role="materialtags"
to your input field to automatically change it to a tags input field.
statement | returns |
---|---|
$("input").val() |
|
$("input").materialtags('items') |
|
Just add data-role="materialtags"
to your input field to automatically change it to a tags input field.
statement | returns |
---|---|
$("input").val() |
|
$("input").materialtags('items') |
|
Typeahead is not included in MaterializeCSS and Materialize Tags. Therefore, if you which to use the functionality offered by TypeAhead, you will have to
include your own typeahead library.
The original version of typeahead.js was developed by twitter, unfortunately
this project is unmaintained for more than two years. Thus, we have choosen to use the forked project by the CoreJavascript Team:
Go to Github Repository.
All functions works seamlessly with the latest version: v1.2.1.
Warning: New functionalities (e.g. autoselect) are only available using TypeAhead 1.2.0 or more recent.
The latest release file can be download from this link: go to GitHub
Typeahead allows you to propose a list of valid tags that the user can choose. Very useful to recommend a list of values to user or restrict him to certain values.
Here is an example below:
<input type="text" value="Amsterdam,Washington"/>
<script>
var citynames = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: 'assets/citynames.json',
filter: function(list) {
return $.map(list, function(cityname) {
return { name: cityname };
});
}
}
});
citynames.initialize();
$('input').materialtags({
typeaheadjs: {
name: 'citynames',
displayKey: 'name',
valueKey: 'name',
source: citynames.ttAdapter()
}
});
</script>
statement | returns |
---|---|
$("input").val() |
|
$("input").materialtags('items') |
|
In addition to proposing a list of values, Typeahead can highlight the matching pattern in the list of recommendation displayed.
Here is an example below:
<input type="text" value="Amsterdam,Washington"/>
<script>
var citynames = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: 'assets/citynames.json',
filter: function(list) {
return $.map(list, function(cityname) {
return { name: cityname };
});
}
}
});
citynames.initialize();
$('input').materialtags({
typeaheadjs: [{
highlight : true,
},
{
name : 'citynames',
displayKey : 'name',
valueKey : 'name',
source : citynames.ttAdapter()
}]
});
</script>
statement | returns |
---|---|
$("input").val() |
|
$("input").materialtags('items') |
|
In addition to proposing a list of values and highlight the matching pattern, Typeahead can automatically select the first proposition.
Here is an example below:
<input type="text" value="Amsterdam,Washington"/>
<script>
var citynames = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: 'assets/citynames.json',
filter: function(list) {
return $.map(list, function(cityname) {
return { name: cityname };
});
}
}
});
citynames.initialize();
$('input').materialtags({
typeaheadjs: [{
autoselect : true,
highlight : true,
},
{
name : 'citynames',
displayKey : 'name',
valueKey : 'name',
source : citynames.ttAdapter()
}]
});
</script>
statement | returns |
---|---|
$("input").val() |
|
$("input").materialtags('items') |
|
Instead of just adding strings as tags, bind objects to your tags. This makes it possible to set id values in your input field's value, instead of just the tag's text.
<input type="text" />
<script>
var cities = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'assets/cities.json'
});
cities.initialize();
var elt = $('input');
elt.materialtags({
itemValue: 'value',
itemText: 'text',
typeaheadjs: {
name: 'cities',
displayKey: 'text',
source: cities.ttAdapter()
}
});
elt.materialtags('add', { "value": 1 , "text": "Amsterdam" , "continent": "Europe" });
elt.materialtags('add', { "value": 4 , "text": "Washington" , "continent": "America" });
elt.materialtags('add', { "value": 7 , "text": "Sydney" , "continent": "Australia" });
elt.materialtags('add', { "value": 10, "text": "Beijing" , "continent": "Asia" });
elt.materialtags('add', { "value": 13, "text": "Cairo" , "continent": "Africa" });
</script>
statement | returns |
---|---|
$("input").val() |
|
$("input").materialtags('items') |
|
option | description | |
---|---|---|
tagClass |
Classname for the tags, or a function returning a classname
|
|
itemValue |
When adding objects as tags, itemValue must be set to the name of the property containing the item's value, or a function returning an item's value.
|
|
itemText |
When adding objects as tags, you can set itemText to the name of the property of item to use for a its tag's text. You may also provide a function which returns an item's value. When this options is not set, the value of
|
|
confirmKeys |
Array of keycodes which will add a tag when typing in the input. (default: [13, 188], which are ENTER and comma)
|
|
maxTags |
When set, no more than the given number of tags are allowed to add (default: undefined). When maxTags is reached, a class 'bootstrap-materialtags-max' is placed on the materialtags element.
|
|
maxChars |
Defines the maximum length of a single tag. (default: undefined)
|
|
trimValue |
When true, automatically removes all whitespace around tags. (default: false)
|
|
allowDuplicates |
When true, the same tag can be added multiple times. (default: false)
|
|
allowTabOnEmpty |
When true, the behavior of tab key is not overridden only if no text is entered after create tags or when the input has no tags (default: false)
|
|
freeInput |
Allow creating tags which are not returned by typeahead's source (default: true)
This is only possible when using string as tags. When itemValue option is set, this option will be ignored.
|
|
typeahead |
Object containing typeahead specific options |
|
source |
An array (or function returning a promise or array), which will be used as source for a typeahead.
|
|
cancelConfirmKeysOnEmpty |
Boolean value controlling whether form submissions get processed when pressing enter in a field converted to a materialtags (default: false).
|
|
onTagExists |
Function invoked when trying to add an item which allready exists. By default, the existing tag hides and fades in.
|
method | description |
---|---|
add |
Adds a tag
Optionally, you can pass a 3rd parameter (object or value) to the add method to gain more control over the process. The parameter is exposed in the options attribute of the event.
Usage:
|
remove |
Removes a tag
Optionally, you can pass a 3rd parameter (object or value) to the remove method to gain more control over the process. The parameter is exposed in the options attribute of the event.
Usage:
|
removeAll |
Removes all tags
|
focus |
Sets focus in the materialtags
|
input |
Returns the materialtags's internal <input />, which is used for adding tags. You could use this to add your own typeahead behaviour for example.
|
refresh |
Refreshes the tags input UI. This might be usefull when you're adding objects as tags. When an object's text changes, you'll have to refresh to update the matching tag's text.
|
destroy |
Removes materialtags behaviour
|
event | description |
---|---|
itemAddedOnInit |
During initialization, pre-defined tags being added will cause this event to be triggered. Example:
|
beforeItemAdd |
Triggered just before an item gets added. Example:
|
itemAdded |
Triggered just after an item got added. Example:
|
beforeItemRemove |
Triggered just before an item gets removed. Example:
|
itemRemoved |
Triggered just after an item got removed. Example:
|