ElementUI实现表单中删除功能(单个删除 批量删除)

64 篇文章 3 订阅
29 篇文章 0 订阅

在这里插入图片描述
批量删除时,必须至少选中一个多选框,否则需要提示未选中

批量删除使用多选框请参考

批量删除按钮

<el-button @click="batchDelNotice()" v-if="userRole==1" type="danger" size="mini" round><i
        class="el-icon-delete"></i> 批量删除
</el-button>

单个删除按钮

<el-table-column
        label="操作"
        fixed="right"
        :render-header="tableAction"
        width="120"
        align="center">
    <template slot-scope="scope">
        <!--单个编辑按钮-->
        <el-button @click="editModifyNotice(scope.row)" type="primary" icon="el-icon-edit" size="small"
                   circle></el-button>
        <!--单个删除按钮-->           
        <el-button @click="deleteNotice(scope.row.noticeId)" v-if="userRole==1" type="danger"
                   icon="el-icon-delete" size="small"
                   circle></el-button>
    </template>
</el-table-column>

使用解释

v-if="userRole==1"  
为判断是否的超级管理员 只有超级管理员才有删除权限
在用户登录后,将用户角色存入sessionStorage中;在进入本页面时,在mounted中获取即可判断

批量删除函数方法

 // 批量删除
batchDelNotice() {
    let app = this;
    if (this.multipleSelection == '') {
        app.$notify({
            title: '温馨提示:',
            message: '您未选中公告,请重新操作!!!',
            type: 'warning'
        });
        return;
    }
    let checkArr = app.multipleSelection;  // multipleSelection存储了勾选到的数据
    let ids = '';
    checkArr.forEach(function (item) {
        ids += item.noticeId + ','; // 添加所有需要删除数据的id到一个数组,post提交过去
    })
    let params = {
        ids: ids
    }
    app.$confirm('确定删除这些公告吗?', '批量删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        app.$Api.deleteBatchNotice(params, function (result) {
            if (result.result == "true") {
                app.$notify({
                    title: '温馨提示:',
                    message: '批量公告' + result.message,
                    type: 'success'
                });
                app.queryNoticeList();
                app.multipleSelection = '';
            } else {
                app.$notify.error({
                    title: '温馨提示:',
                    message: '批量公告' + result.message
                });
            }
        });
    }).catch(() => {
        this.$message({
            type: 'info',
            message: '已取消删除'
        });
    });
}

单个删除函数方法

  // 单个删除
deleteNotice(noticeId) {
     let app = this;
     var deleteData = {
         noticeId: noticeId
     }
     app.$confirm('确定删除该公告吗', '删除公告', {
         confirmButtonText: '确定',
         cancelButtonText: '取消',
         type: 'error'
     }).then(() => {
         app.$Api.deleteNotice(deleteData, function (result) {
             if (result.result == "true") {
                 app.$notify({
                     title: '温馨提示:',
                     message: '公告' + result.message,
                     type: 'success'
                 });
                 app.queryNoticeList();
             } else {
                 app.$notify.error({
                     title: '温馨提示:',
                     message: '公告' + result.message
                 });
             }
         });
     }).catch(() => {
         //取消操作
         app.$message({
             type: 'info',
             message: '已取消删除'
         });
     });
 },
  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 ElementUI 的 Table 组件实现批量删除的示例代码: ```vue <template> <div> <el-button type="danger" @click="deleteSelected">批量删除</el-button> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="danger" size="small" @click="deleteItem(scope.row.id)">删除</el-button> </template> </el-table-column> </el-table> </div> </template> <script> import axios from 'axios' export default { data() { return { tableData: [] } }, mounted() { this.fetchData() }, methods: { fetchData() { axios.get('/api/items').then(res => { this.tableData = res.data }) }, deleteItem(id) { axios.delete(`/api/items/${id}`).then(() => { this.fetchData() }) }, deleteSelected() { const selectedIds = this.$refs.table.selection.map(item => item.id) axios.delete('/api/items', { data: { ids: selectedIds } }).then(() => { this.$refs.table.clearSelection() this.fetchData() }) } } } </script> ``` 这个示例代码假设你已经有一个 `/api/items` 接口可以获取所有的项,并且可以通过 DELETE 方法来删除一个或多个项。其中,`$refs.table` 是 Table 组件的引用,`selection` 属性存储了当前选中的项的数据。在 `deleteSelected` 方法中,我们使用了 axios 的 `delete` 方法,将选中的项的 ID 作为参数传递给后端。注意,在这里我们也使用了 `data` 选项来指定传递的数据。在成功删除后,我们使用了 `clearSelection` 方法来清空选中的项,然后再重新获取数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值