vue多html标签,Vue实现多标签选择器

本文实例为大家分享了Vue实现多标签选择器展示的具体代码,供大家参考,具体内容如下

实现效果

176ec26c901557e6f63e852f4ec81cce.png

实现代码

Document

.not-active {

display: inline-block;

font-size: 12px;

margin: 5px 8px;

}

span {

margin: 0 2px;

}

{{category.name}}:

不限

不限

{{ child.name }}

{{ child.name }}

清空已选:

v-for='(condition, index) in conditions'

:key="condition.id"

type="primary"

:closable="true"

size="small"

:disable-transitions="true"

@close='removeCondition(condition, index)'

@click='removeCondition(condition, index)'>

{{condition.name}}

// 简单封装一个公用组件

Vue.component('my-tag', {

template: "",

methods: {

clickChild() {

this.$emit("click-child")

}

}

});

var app = new Vue({

el: '#app',

data() {

return {

categories, // 分类标签,可从外部加载配置

conditions: [] // 已选条件

}

},

watch: {

// 监听条件变化,按照请求接口拼装请求参数

conditions(val){

let selectedCondition = {};

for(let categorie of this.categories){

let selected_list = [];

for(let child of categorie.children){

if(child.active){

selected_list.push(child.name);

}

}

selectedCondition[categorie.name] = selected_list.join("|")

}

console.log(selectedCondition);

}

},

methods: {

// 处理标签点击事件,未选中则选中,已选中则取消选中

clickChild(category, categoryIndex, child, childIndex) {

let uid = `${categoryIndex}-${childIndex}`

child.uid = uid;

console.log(uid)

// 取消选择

if (child.active === true) {

category.count--;

child.active = false;

this.conditions.forEach((conditionChild, index) => {

if (conditionChild.uid === child.uid) {

this.conditions.splice(index, 1);

}

});

// 选择

} else {

category.count++;

child.active = true;

this.conditions.push(child);

}

},

// 清除已选整个类别标签

clearCategory(category, categoryIndex) {

category.count = 0;

// 可选列表均为未选中状态

category.children.forEach(child => {

child.active = false;

})

// 清空该类已选元素

for (let index = this.conditions.length - 1; index >= 0; index--) {

const conditionChild = this.conditions[index];

if (conditionChild.uid.startsWith(categoryIndex)) {

this.conditions.splice(index, 1);

}

}

},

// 移除一个条件

removeCondition(condition, index) {

let categoryIndex = condition.uid.split("-")[0];

this.categories[categoryIndex].count --;

this.conditions.splice(index, 1)

condition.active = false;

},

// 清空所有条件

clearCondition() {

for(let i = this.conditions.length-1; i >=0 ; i--){

this.removeCondition(this.conditions[i], i);

}

}

}

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值