最近做了vue的一个项目,效果要实现多选及反选,数据是从后端传过来的。
代码:
<div class="lableView">
<label
class="lable_one"
:class="{active:sz.includes(index)}"
v-for="(item,index) in lableList"
:key="index"
@click="multiSelect(item,index)"
>{{item.tag_name}}</label>
</div>
</div>
后台请求lable标签的接口:
//请求获得评论时所有标签
getAllLable(){
let that = this;
this.$post("?s=App.Index.Tags").then((res)=>{
console.log(res);
that.lableList = res.data.data;
that.lableList.forEach(function(itemx, index){
that.lableList[index].isActive = false
})
})
},
data里的数据:
data(){
return{
lableList:[],
sz:[],
sz2:[]
}
}
点击选择:
multiSelect(e,index){
if(this.sz.includes(index) || this.sz2.includes(e.tag_name)){
this.sz=this.sz.filter(function (ele){
return ele != index;
});
this.sz2=this.sz2.filter(function (ele){ //要把标签内容获取下来之后传给后端
return ele !=e.tag_name;
});
}else{
if(this.sz.length < 3){
this.sz.push(index);
this.sz2.push(e.tag_name);
this.from.lableTarg=this.sz2.join(',')
}else{
Toast.fail('所选标签不能超过3个');
}
}
},