elementui 之el-table 实现批量删除功能解析

符号 阅读:232 2021-03-31 12:25:28 评论:0

页面前端效果展示:

模板定义(template)

 <el-row> 
            <el-table :data="tableData" style="width: 100%;" ref="multipleTable" @selection-change="handleSelectionChange"> 
                        <el-table-column type="selection" label="序号"> 
                        </el-table-column> 
                        <el-table-column prop="title" label="通知标题" > 
                        </el-table-column> 
                        <el-table-column prop="content" label="通知详情" > 
                        </el-table-column> 
                        <el-table-column prop="addTime" label="添加时间" :formatter="formatDate" > 
                        </el-table-column> 
                         <el-table-column prop="adminId" label="管理员ID" > 
                        </el-table-column> 
                         
                        <el-table-column label="操作"> 
                            <template slot-scope="scope"> 
                                <el-button type="primary" size="mini" @click="toEdit(scope)">编辑</el-button> 
                                <el-button type="danger" size="mini" @click="deleteRole(scope)">删除</el-button> 
                            </template> 
                        </el-table-column> 
                    </el-table> 
                    <br> 
                    <el-pagination 
                        @size-change="handleSizeChange" 
                        @current-change="handleCurrentChange" 
                        :current-page="pagination.pageIndex" 
                        :page-sizes="[5, 10, 20, 30, 40]" 
                        :page-size=pagination.pageSize 
                        layout="total, prev, pager, next" 
                        :total=pagination.total> 
                    </el-pagination> 
        </el-row>

重点代码:在el-table 添加

 <el-table-column type="selection" label="序号"> 
 </el-table-column>

为批量删除按钮添加delArray() 方法,在methods 定义批量删除方法:

  // 批量删除方法 
      delArray: function() { 
        const length = this.multipleSelection.length; 
 
				for (let i = 0; i < length; i++) { 
					  this.delarr.push(this.multipleSelection[i].id); 
        } 
 
        if(this.delarr.length > 0) { 
          var strs = this.delarr.join(",") 
          this.$axios({ 
            method:'post', 
            url:'/api/notice/batchDelete', 
            data:{'ids': strs}, 
            headers:{ 
                'Content-Type':'application/json;charset=utf-8'      //改这里就好了 
            } 
        }).then(res => { 
          this.$message.success('批量删除成功') 
          this.init() 
        }) 
          .catch(function (error) { 
            console.log(error) 
          }) 
        } 
      }, 

SpringBoot + MybatisPlus 批量删除方法:

@ApiOperation(httpMethod = "POST", value = "批量通知删除") 
	@RequestMapping(value = "/batchDelete", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8") 
	public Result delete(@RequestBody Map<String, Object> paramter) { 
		if(paramter.get("ids") != null) { 
			String sids = (String) paramter.get("ids"); 
			if (StringUtils.isNotEmpty(sids)) {			 
				baoanNoticeService.removeByIds(Arrays.asList(sids.split(","))); 
				return Result.ok(); 
			} 
		} 
		 
		return Result.error("请求参数缺失Sids"); 
	}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们