Vue与SpringBoot(二)实现批量删除功能

1、在el-table中增加以下代码

增加属性:

ref="multipleTable" @selection-change="handleSelectionChange"

添加一列:

<el-table-column type="selection" width="55"></el-table-column>

2、在表格上方增加按钮代码

<el-button class="small" type="warning" size="small" @click="removeBatch()">批量删除</el-button>

如下图:

3、data中增加

data () {
    return {
      tableData: [],
      keywords:'',
      logIds:'',
      msg:'',//记录每一条的信息,便于取id
      multipleSelection:[],//多选的数据
      currentPage: 1,
      pagesize: 10
    }
  }

4、methods中增加两个函数

handleSelectionChange(val) {
    this.multipleSelection = val;
    },
    removeBatch () {
      const length = this.multipleSelection.length;
        alert(length);
        for (let i = 0; i < length; i++) {
          this.logIds += this.multipleSelection[i].logId+','
        }
      this.$confirm('此操作将批量删除日志信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {        
        this.$axios
          .post('/loginfo/deleteBatchLogs', {logIds: this.logIds}).then(resp => {
            if (resp && resp.status === 200) {
              this.loadLogInfo()
            }
          })
      }
      ).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

5、后台代码Controller中

/**
     * 批量删除
     * @param request
     * @param response
     */
    @PostMapping("/api/loginfo/deleteBatchLogs")
    public String deleteBatchLogs(@RequestBody LogInfo logInfo) {
        try {
            System.out.println("logInfo.getLogIds():"+logInfo.getLogIds());
            String[] ids = logInfo.getLogIds().split(",");
            for (String id : ids) {
                logInfo.setLogId(Long.parseLong(id));
                logInfoService.logDeleteById(logInfo);
            }
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

其他dao以及mapper中省略了。

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值