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

页面前端效果展示:

模板定义(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");
	}

 

  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-tableElementUI 中的一个表格组件,如果想要实现可编辑功能,可以使用以下两种方式: 1. 使用 el-editable-table 组件 el-editable-table 组件是 ElementUI 中专门为表格编辑而设计的组件,可以轻松实现单元格编辑、行编辑和批量编辑等功能。 使用方法如下: 首先需要在页面中引入 el-editable-table 组件: ``` import ElEditableTable from 'el-editable-table' Vue.use(ElEditableTable) ``` 然后在 el-table 标签中使用 el-editable-table 组件即可: ``` <el-editable-table :data="tableData"> <el-table-column prop="name" label="姓名" :editable="true"></el-table-column> <el-table-column prop="age" label="年龄" :editable="true"></el-table-column> <el-table-column prop="sex" label="性别" :editable="true"></el-table-column> </el-editable-table> ``` 2. 自行编写编辑功能代码 如果不想使用 el-editable-table 组件,也可以自行编写代码实现表格编辑功能。具体实现方法如下: 首先在 el-table 标签中添加 :highlight-current-row="true" 属性,这样可以高亮当前行。 然后在 el-table-column 标签中添加 scoped-slot="default" 属性,用于自定义单元格内容。 接着在 methods 中添加 edit 方法,用于切换单元格为编辑状态。 最后在模板中使用 v-if 判断单元格是否为编辑状态,并根据情况显示不同的内容即可。 具体代码示例如下: ``` <el-table :data="tableData" :highlight-current-row="true"> <el-table-column prop="name" label="姓名" :formatter="nameFormatter" :editable="true" scoped-slot="default"></el-table-column> <el-table-column prop="age" label="年龄" :editable="true" scoped-slot="default"></el-table-column> <el-table-column prop="sex" label="性别" :editable="true" scoped-slot="default"></el-table-column> </el-table> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, sex: '男' }, { name: '李四', age: 22, sex: '女' }, { name: '王五', age: 25, sex: '男' } ], editCell: null } }, methods: { nameFormatter(row, column) { return this.editCell === `${row.name}-${column.property}` ? <el-input v-model={row[column.property]} size="mini" @blur={this.editCell = null}></el-input> : row[column.property]; }, edit(row, column) { this.editCell = `${row.name}-${column.property}`; this.$nextTick(() => { this.$refs[this.editCell][0].$refs.input.focus(); }); } } } </script> ``` 通过上述两种方式,都可以实现 el-table 可编辑的功能。具体选哪种方式,可以根据实际情况进行选择

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值