Hi friends I'm using this source Tags input for my project , but I require only 5 tags to be entered after that it should not allow the tags to be entered into the input field . The input field can now receive unlimited tags,But I do not know how to limit it
please guide me
Thanks in advance.
function tagSelect() {
return {
open: false,
textInput: '',
tags: [],
init() {
this.tags = JSON.parse(this.$el.parentNode.getAttribute('data-tags'));
},
addTag(tag) {
tag = tag.trim()
if (tag != "" && !this.hasTag(tag)) {
this.tags.push( tag )
}
this.clearSearch()
this.$refs.textInput.focus()
this.fireTagsUpdateEvent()
},
fireTagsUpdateEvent() {
this.$el.dispatchEvent(new CustomEvent('tags-update', {
detail: { tags: this.tags },
bubbles: true,
}));
},
hasTag(tag) {
var tag = this.tags.find(e => {
return e.toLowerCase() === tag.toLowerCase()
})
return tag != undefined
},
removeTag(index) {
this.tags.splice(index, 1)
this.fireTagsUpdateEvent()
},
search(q) {
if ( q.includes(",") ) {
q.split(",").forEach(function(val) {
this.addTag(val)
}, this)
}
this.toggleSearch()
},
clearSearch() {
this.textInput = ''
this.toggleSearch()
},
toggleSearch() {
this.open = this.textInput != ''
}
}
}
You could update the fireTagsUpdateEvent() function to disable the input when there are 5 (or more) tags
fireTagsUpdateEvent() {
this.$refs.textInput.setAttribute('disabled', this.tags.length >= 5)
this.$el.dispatchEvent(new CustomEvent('tags-update', {
detail: {
tags: this.tags
},
bubbles: true,
}));
},