vue.js - Bootstarp Vue - 表单标签 - 如何知道从列表中删除了哪个标签?
问题描述
嗨,我是 Vue 的新手,我想问一下 From Tag 默认情况下,我用所有类别的列表填充 Form-tag 列表,如果用户从标签列表中删除任何类别(标签),我想知道他删除了哪一个...
我检查了 Bootstrap vue 文档,但他们得到了新的标签 id 和新的标签值,没有提到任何关于选定和删除的内容。
<b-form-tags input-id="tags-basic"
v-model="selectedCat"
class="col-12"
placeholder="Selected Categories"
@@input="appCatFilter()"
>
</b-form-tags>
解决方案
您可以使用 awatcher
来观察您的b-form-tags
v-model,以捕捉对其所做的任何更改。
观察者传入新值和旧值。因此,如果您比较两者,您可以找到删除的内容。
new Vue({
el: "#app",
data() {
return {
value: ["apple", "orange"]
};
},
watch: {
value(newVal, oldVal) {
/* Finds the value(s) that got removed */
const removed = oldVal.filter(v => !newVal.includes(v));
if(removed && removed.length > 0) {
alert(`${removed} was removed from the list`);
/* Do something here */
}
}
}
});
<link href="https://unpkg.com/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/bootstrap-vue@2.14.0/dist/bootstrap-vue.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.14.0/dist/bootstrap-vue.js"></script>
<div id="app">
<label for="tags-basic">Type a new tag and press enter</label>
<b-form-tags input-id="tags-basic" v-model="value" class="mb-2"></b-form-tags>
<p>Value: {{ value }}</p>
</div>
推荐阅读
- c# - System.Net 下缺少 IPEndPointCollection
- vue.js - 如何使用 Vuejs 和 Laravel 应用数据表
- javascript - 当溢出设置为隐藏时,ScrollTop 在 reactjs 中不起作用
- node.js - 猫鼬异步/等待问题
- javascript - 是什么让“数组”与“对象”不同?
- r - 如何将分布拟合到ggplot中的以下条形图
- tensorflow-lite - CoreMl 是否支持输出长特征而不是最终预测?
- python - 如何在 Python 的同一页面上绘制不同的图?
- java - 如何使用 Selenium 和 Java 在谷歌搜索结果页面上单击带有文本的元素作为谷歌的 Gmail
- amazon-web-services - 尝试使用无服务器创建 AWS Elasticsearch 集群。以状态码结束:409;错误代码:InvalidTypeException