通过单击 Add 按钮,按 ENTER 键或在 change 事件触发新标签输入时(例如,当焦点从输入移开时)添加标签。仅当用户输入新标签值时,Add 按钮才会出现。
默认渲染:
apple
orange
Value: [
"apple",
"orange"
]
<template><div><labelfor="tags-basic">Type a new tag and press enter</label><b-form-tagsinput-id="tags-basic"v-model="value"class="mb-2"></b-form-tags><p>Value: {{ value }}</p></div></template><script>exportdefault {
data() {
return {
value: ['apple', 'orange']
}
}
}
</script>
您可以通过 no-add-on-enter 属性在按 ENTER 时禁用添加新标签,并可以通过 add-on-change 属性在输入的 change 事件上添加标签。
使用分隔符创建标签
要在键入分隔符时自动创建标签(例如 SPACE ,, 等),请将 separator 属性设置为将触发添加标签的字符。如果需要多个分隔符,则将它们作为单个字符串(即' ,;')或字符数组(即 [' ', ',', ';'] )包含在内,这将触发一个新标签在 SPACE ,, ,或; 时添加 键入)。分隔符必须是单个字符。
以下示例将在 SPACE ,, ,或 ; 时自动创建标签。键入:
one
two
Value: [
"one",
"two"
]
<template><div><labelfor="tags-separators">Enter tags separated by space, comma or semicolon</label><b-form-tagsinput-id="tags-separators"v-model="value"separator=" ,;"placeholder="Enter new tags separated by space, comma or semicolon"no-add-on-enterclass="mb-2"
></b-form-tags><p>Value: {{ value }}</p></div></template><script>exportdefault {
data() {
return {
value: ['one', 'two']
}
}
}
</script>
通过 backspace 按键删除最后一个标签
当设置了 remove-on-delete 属性后,当用户按下BACKSPACE( 或 DEL )并且 输入值为空时,标签列表中的最后一个标签将被删除。
apple
orange
grape
Press BACKSPACE to remove the last tag entered
Value: [
"apple",
"orange",
"grape"
]
<template><div><labelfor="tags-remove-on-delete">Enter new tags separated by space</label><b-form-tagsinput-id="tags-remove-on-delete":input-attrs="{ 'aria-describedby': 'tags-remove-on-delete-help' }"v-model="value"separator=" "placeholder="Enter new tags separated by space"remove-on-deleteno-add-on-enterclass="mb-2"
></b-form-tags><b-form-textid="tags-remove-on-delete-help">
Press <kbd>BACKSPACE</kbd> to remove the last tag entered
</b-form-text><p>Value: {{ value }}</p></div></template><script>exportdefault {
data() {
return {
value: ['apple', 'orange', 'grape']
}
}
}
</script>
样式选项
有几个属性可以用来更改默认标记接口的基本样式:
属性
描述
tag-pills
呈现带有胶囊外观的标签
tag-variant
将 Bootstrap 上下文相关主题颜色之一应用于标签
size
设置组件外观的大小。'sm','md'(默认)或'lg'
placeholder
新标签输入元素的占位符文本
state
设置控件的上下文状态。 设置为 true(有效),false(无效)或 null
disabled
将组件置于禁用状态
有关其他属性,请参阅此页面底部的组件参考部分。
组件的焦点和验证状态样式取决于 BootstrapVue 的自定义 CSS 。
apple
orange
grape
Value: [
"apple",
"orange",
"grape"
]
<template><div><labelfor="tags-pills">Enter tags</label><b-form-tagsinput-id="tags-pills"v-model="value"tag-variant="primary"tag-pillssize="lg"separator=" "placeholder="Enter new tags separated by space"class="mb-2"
></b-form-tags><p>Value: {{ value }}</p></div></template><script>exportdefault {
data() {
return {
value: ['apple', 'orange', 'grape']
}
}
}
</script>
与本机浏览器 <form> 提交一起使用
除非您通过 name 属性提供名称,否则不会通过标准表单 action【操作】提交带标记的输入的值。提供名称后,<b-form-tags> 将为每个标签创建一个隐藏的 <input> 。隐藏的输入会将 name 属性设置为 name 属性的值。
You must provide at least 3 tags and no more than 8
Tags must be 3 to 5 characters in length and all lower
case. Enter tags separated by spaces or press enter.
<template><div><b-form-group:state="state"label="Tags validation example"label-for="tags-validation"><b-form-tagsinput-id="tags-validation":input-attrs="{ 'aria-describedby': 'tags-validation-help' }"v-model="tags":state="state":tag-validator="tagValidator"separator=" "
></b-form-tags><!-- The following slots are for b-form-group --><templatev-slot:invalid-feedback>
You must provide at least 3 tags and no more than 8
</template><templatev-slot:description><divid="tags-validation-help">
Tags must be 3 to 5 characters in length and all lower
case. Enter tags separated by spaces or press enter.
</div></template></b-form-group></div></template><script>exportdefault {
data() {
return {
tags: [],
dirty: false
}
},
computed: {
state() {
// Overall component validation statereturnthis.dirty ? (this.tags.length > 2 && this.tags.length < 9) : null
}
},
watch: {
tags(newVal, oldVal) {
// Set the dirty flag on first change to the tags arraythis.dirty = true
}
},
methods: {
tagValidator(tag) {
// Individual tag validator functionreturn tag === tag.toLowerCase() && tag.length > 2 && tag.length < 6
}
}
}
</script>
<template><div><b-form-tagsv-model="value"no-outer-focusclass="mb-2"><templatev-slot="{ tags, inputAttrs, inputHandlers, addTag, removeTag }"><b-input-grouparia-controls="my-custom-tags-list"><inputv-bind="inputAttrs"v-on="inputHandlers"placeholder="New tag - Press enter to add"class="form-control"><b-input-group-append><b-button @click="addTag()"variant="primary">Add</b-button></b-input-group-append></b-input-group><ulid="my-custom-tags-list"class="list-unstyled d-inline-flex flex-wrap mb-0"aria-live="polite"aria-atomic="false"aria-relevant="additions removals"
><!-- Always use the tag value as the :key, not the index! --><!-- Otherwise screen readers will not read the tag
additions and removals correctly --><b-cardv-for="tag in tags":key="tag":id="`my-custom-tags-tag_${tag.replace(/\s/g, '_')}_`"tag="li"class="mt-1 mr-1"body-class="py-1 pr-2 text-nowrap"
><strong>{{ tag }}</strong><b-button
@click="removeTag(tag)"variant="link"size="sm":aria-controls="`my-custom-tags-tag_${tag.replace(/\s/g, '_')}_`"
>remove</b-button></b-card></ul></template></b-form-tags></div></template><script>exportdefault {
data() {
return {
value: ['apple', 'orange', 'banana', 'pear', 'peach']
}
}
}
</script>
使用自定义表单组件
范围包含可以直接绑定到大多数自定义输入或选择组件的属性和事件处理程序(事件处理程序接受字符串标记值 或 本机事件对象)。键入任何作为字符发出 input【输入】的组件,并且(可选)在输入值更改时(即,在模糊或选择时)发出 change【更改】并使用 value 属性作为 v-model 的组件都应工作而无需进行修改。
<templatev-slot:default="{ inputAttrs, inputHandlers, removeTag, tags }"><custom-input:id="inputAttrs.id":vistom-value-prop="inputAttrs.value"
@custom-input-event="inputHandlers.input($event)"
@custom-change-event="inputHandlers.change($event)"
@keydown.native="inputHandlers.keydown($event)"
></custom-input><templatev-for="tag in tags"><!-- Your custom tag list here --></template></template>
<template><div><b-form-checkboxswitchsize="lg"v-model="disabled">Disable</b-form-checkbox><b-form-tagsv-model="value"
@input="resetInputValue()"tag-variant="success"class="mb-2 mt-2":disabled="disabled"no-outer-focusplaceholder="Enter a new tag value and click Add":state="state"
><templatev-slot="{tags, inputId, placeholder, disabled, addTag, removeTag }"><b-input-group><!-- Always bind the id to the input so that it can be focused when needed --><b-form-inputv-model="newTag":id="inputId":placeholder="placeholder":disabled="disabled":formatter="formatter"
></b-form-input><b-input-group-append><b-button @click="addTag(newTag)":disabled="disabled"variant="primary">Add</b-button></b-input-group-append></b-input-group><b-form-invalid-feedback:state="state">
Duplicate tag value cannot be added again!
</b-form-invalid-feedback><ulv-if="tags.length > 0"class="mb-0"><liv-for="tag in tags":key="tag":title="`Tag: ${tag}`"class="mt-2"><spanclass="d-flex align-items-center"><spanclass="mr-2">{{ tag }}</span><b-button:disabled="disabled"size="sm"variant="outline-danger"
@click="removeTag(tag)"
>
remove tag
</b-button></span></li></ul><b-form-textv-else>
There are no tags specified. Add a new tag above.
</b-form-text></template></b-form-tags></div></template><script>exportdefault {
data() {
return {
newTag: '',
disabled: false,
value: []
}
},
computed: {
state() {
// Return false (invalid) if new tag is a duplicatereturnthis.value.indexOf(this.newTag.trim()) > -1 ? false : null
}
},
methods: {
resetInputValue() {
this.newTag = ''
},
formatter(value) {
return value.toUpperCase()
}
}
}
</script>
以下是使用 <b-dropdown> 组件从一组预定义标签中进行选择或搜索的示例:
<template><div><b-form-grouplabel="Tagged input using dropdown"><b-form-tagsv-model="value"no-outer-focusclass="mb-2"><templatev-slot="{ tags, disabled, addTag, removeTag }"><ulv-if="tags.length > 0"class="list-inline d-inline-block mb-2"><liv-for="tag in tags":key="tag"class="list-inline-item"><b-form-tag
@remove="removeTag(tag)":title="tag":disabled="disabled"variant="info"
>{{ tag }}</b-form-tag></li></ul><b-dropdownsize="sm"variant="outline-secondary"blockmenu-class="w-100"><templatev-slot:button-content><b-iconicon="tag-fill"></b-icon> Choose tags
</template><b-dropdown-form @submit.stop.prevent="() => {}"><b-form-grouplabel-for="tag-search-input"label="Search tags"label-cols-md="auto"class="mb-0"label-size="sm":description="searchDesc":disabled="disabled"
><b-form-inputv-model="search"id="tag-search-input"type="search"size="sm"autocomplete="off"
></b-form-input></b-form-group></b-dropdown-form><b-dropdown-divider></b-dropdown-divider><b-dropdown-item-buttonv-for="option in availableOptions":key="option"
@click="onOptionClick({ option, addTag })"
>
{{ option }}
</b-dropdown-item-button><b-dropdown-textv-if="availableOptions.length === 0">
There are no tags available to select
</b-dropdown-text></b-dropdown></template></b-form-tags></b-form-group></div></template><script>exportdefault {
data() {
return {
options: ['Apple', 'Orange', 'Banana', 'Lime', 'Peach', 'Chocolate', 'Strawberry'],
search: '',
value: []
}
},
computed: {
criteria() {
// Compute the search criteriareturnthis.search.trim().toLowerCase()
},
availableOptions() {
const criteria = this.criteria
// Filter out already selected optionsconst options = this.options.filter(opt =>this.value.indexOf(opt) === -1)
if (criteria) {
// Show only options that match criteriareturn options.filter(opt => opt.toLowerCase().indexOf(criteria) > -1);
}
// Show all options availablereturn options
},
searchDesc() {
if (this.criteria && this.availableOptions.length === 0) {
return'There are no tags matching your search criteria'
}
return''
}
},
methods: {
onOptionClick({ option, addTag }) {
addTag(option)
this.search = ''
}
}
}
</script>