ページ効果
データ構造
list: [{
name: '名称1',
indexs: [{
tag: '标签1'
},{
tag: '标签2'
},{
tag: '标签3'
},{
tag: '标签4'
}]
},{
name: '名称2',
indexs: [{
tag: '标签11'
},{
tag: '标签22'
},{
tag: '标签33'
},{
tag: '标签44'
},{
tag: '标签55'
},{
tag: '标签66'
}]
},{
name: '名称3',
indexs: [{
tag: '标签111'
},{
tag: '标签222'
}]
},{
name: '名称4',
indexs: [{
tag: '标签1111'
},{
tag: '标签2222'
},{
tag: '标签3333'
}]
},{
name: '名称5',
indexs: [{
tag: '标签1'
}]
}]
実現機能
右側の複数選択ボックスを選択した後、それらを一括で削除します。ローカル実装用のインターフェイスはありません。
問題に遭遇する
最初は foreach ループを選択すると削除されるとばかり思っていて、foreach+splice() を使用したところ、以下の問題に遭遇しました。その時は思ってたんですが…)
// 最开始写的删除方法
deleteBtn() {
this.list.forEach((i) =>{
i.indexs.forEach((c,cIndex) =>{
if(c.checked == true) {
console.log(c.checked);
i.indexs.splice(cIndex,1)
}
});
});
}
解決
this.list.forEach((i) =>{
// 删除选中对象
i.indexs = i.indexs.filter(item => {
return !item.checked
});
});
// 该名称下标签为空删除名称
this.list = this.list.filter(item2 => {
return item2.indexs.length > 0;
});
ページの全体のコードは次のとおりです
<template>
<div style="width: 50%;margin: 0 auto">
<div align="right">
<button @click="deleteBtn()">删除指标</button>
</div>
<ul class="bq-title">
<li>名称</li>
<li>标签</li>
</ul>
<ul class="bq-con" v-for="(item,index) in list" :key="index">
<li :style="{lineHeight:`${
30*item.indexs.length}px`}">
<span>{
{
item.name}}</span>
</li>
<li>
<div v-for="(iItem,iIndex) in item.indexs" :key="iIndex">
<span class="mb-5" :title="iItem.tag">{
{
iItem.tag}}</span>
<el-checkbox v-model="iItem.checked" class="checkbox" ref="checkbox"></el-checkbox>
</div>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'home',
data() {
return {
list: [{
name: '名称1',
indexs: [{
tag: '标签1'
},{
tag: '标签2'
},{
tag: '标签3'
},{
tag: '标签4'
}]
},{
name: '名称2',
indexs: [{
tag: '标签11'
},{
tag: '标签22'
},{
tag: '标签33'
},{
tag: '标签44'
},{
tag: '标签55'
},{
tag: '标签66'
}]
},{
name: '名称3',
indexs: [{
tag: '标签111'
},{
tag: '标签222'
}]
},{
name: '名称4',
indexs: [{
tag: '标签1111'
},{
tag: '标签2222'
},{
tag: '标签3333'
}]
},{
name: '名称5',
indexs: [{
tag: '标签1'
}]
}]
}
},
methods:{
deleteBtn() {
// 最开始的写法
// this.list.forEach((i) =>{
// i.indexs.forEach((c,cIndex) =>{
// if(c.checked == true) {
// i.indexs.splice(cIndex,1)
// }
// });
// });
this.list.forEach((i) =>{
// 删除选中对象
i.indexs = i.indexs.filter(item => {
return !item.checked
});
});
// 该名称下标签为空删除名称
this.list = this.list.filter(item2 => {
return item2.indexs.length > 0;
});
// 取消多选框选中
this.$refs.checkbox.forEach((i) =>{
if(i.model == true) {
i.model =false;
}
})
},
},
}
</script>
<style lang="less" scoped>
button{
background: red;
color: white;
border: none;
padding: 10px 30px;
border-radius: 10px;
margin-bottom: 20px;
}
.bq-title {
overflow: hidden;
background: #849f9c;
height: 30px;
line-height: 30px;
color: white;
li{
float: left;
width: 45%;
text-align: center;
}
}
.bq-con{
background: #eff9f8;
border-radius: 5px;
padding: 5px;
margin-top: 15px;
overflow: hidden;
.checkbox{
margin-left: 3px;
}
li{
float: left;
width: 45%;
text-align: center;
div{
height: 28px;
margin: 0 auto 5px auto;
}
span{
font-size: 14px;
border-radius: 5px;
height: 28px;
line-height: 28px;
width: 80%;
background: #c5dad8;
display: inline-block;
}
.mb-5{
margin: 0 auto 5px auto;
}
}
}
</style>