vue实现批量删除

<template>
<el-button size="medium" @click="Tips">删除</el-button>
      <el-dialog title="删除提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <span>确认删除信息?</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="toggleSelection">取 消</el-button>
          <el-button type="primary" @click="Delete()">确 定</el-button>
        </span>
      </el-dialog>
</template>
<script>
  import BaseApi from "@/api/index"
  import commonImages from "@/static/js/commonMedium.js";
  import axios from "axios";
  import qs from 'qs';
  methods:{
     Tips() {
        this.deleteForm = this.multipleSelection;
        if (this.deleteForm.length == 0) {
          this.$message.error('请选择要删除的数据');
        } else {
          this.dialogVisible = true;
        }

       },
      handleClose(done) {
        this.$confirm('确认关闭?')
          .then(_ => {
            done();
          })
          .catch(_ => { });
        // this.toggleSelection();
      },
       toggleSelection() {
        this.dialogVisible = false;
        if (this.deleteForm) {
          this.$nextTick(() => {
            this.deleteForm.forEach(row => {
              console.log(row);
              this.$refs.multipleTable.toggleRowSelection(row);
            });
          })
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      //删除
      Delete() {
        const _this = this;
        this.dialogVisible = false;
        if (this.deleteForm.length == 0) {
          this.$message.error('请选择要删除的数据');
        }
        else {
          //this.dialogVisible=true;
          axios.post('/havery/demo/deleteByID', this.deleteForm, { indices: false,                   headers: { 'Content-Type': 'application/json' } }).then(result => {
            if (result.data) {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.onSubmit(this.formInline.co_name);
            }
            else {
              this.$message.error('删除失败');
              return false;
            }
          })
        }
      },
      //选中的项
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
   },
   data:{
     return:{
        dialogVisible: false,
        deleteForm: [],//删除的项
        multipleSelection: [],//复选框
     }
  }
</script>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现批量删除,可以按照以下步骤进行: 1. 前端页面中,勾选需要删除的数据,点击“删除”按钮,将所选数据的id封装成一个数组,通过axios发送到后端。 2. 后端接收前端传来的id数组,根据id数组中的id,从数据库中删除对应数据。 3. 在SpringBoot中,可以使用@DeleteMapping注解来接收前端发送的DELETE请求,并在方法体中实现删除操作。在MyBatisPlus中,可以使用wrapper进行批量删除操作。 4. 最后,后端返回删除结果给前端。 下面是一个简单的代码示例: 前端代码: ``` <template> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> <el-table-column fixed="right" label="操作"> <template slot-scope="scope"> <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button> </template> </el-table-column> </el-table> <el-button type="danger" @click="batchDelete">批量删除</el-button> </div> </template> <script> import axios from 'axios'; export default { data() { return { tableData: [], // 表格数据 multipleSelection: [], // 多选数组 }; }, methods: { // 删除单条数据 handleDelete(id) { axios.delete('/api/delete/' + id).then((res) => { this.getTableData(); }); }, // 批量删除 batchDelete() { const ids = this.multipleSelection.map((item) => item.id); axios.delete('/api/batchDelete', { data: ids, }).then((res) => { this.getTableData(); this.multipleSelection = []; }); }, // 获取表格数据 getTableData() { axios.get('/api/getTableData').then((res) => { this.tableData = res.data; }); }, }, created() { this.getTableData(); }, }; </script> ``` 后端代码: ``` @RestController @RequestMapping("/api") public class UserController { @Autowired private UserService userService; // 删除单条数据 @DeleteMapping("/delete/{id}") public boolean delete(@PathVariable("id") Long id) { return userService.removeById(id); } // 批量删除 @DeleteMapping("/batchDelete") public boolean batchDelete(@RequestBody List<Long> ids) { return userService.removeByIds(ids); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值