VUE多选删除列表遇到的坑

页面效果

在这里插入图片描述

数据结构

        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>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值