当前位置:首页 > VUE

vue element 实现批量删除

2026-01-21 02:46:45VUE

实现批量删除功能

在Vue和Element UI中实现批量删除功能,主要涉及表格多选、批量操作按钮和删除逻辑的实现。

表格多选配置

在Element UI的el-table组件中启用多选功能,需要添加selection列和row-key属性:

vue element 实现批量删除

<el-table
  ref="multipleTable"
  :data="tableData"
  style="width: 100%"
  @selection-change="handleSelectionChange"
  row-key="id">
  <el-table-column
    type="selection"
    width="55">
  </el-table-column>
  <!-- 其他列 -->
</el-table>

处理选中项

通过selection-change事件获取当前选中的行数据:

data() {
  return {
    multipleSelection: []
  }
},
methods: {
  handleSelectionChange(val) {
    this.multipleSelection = val;
  }
}

批量删除按钮

添加批量删除按钮并绑定点击事件:

vue element 实现批量删除

<el-button 
  type="danger" 
  :disabled="!multipleSelection.length" 
  @click="handleBatchDelete">
  批量删除
</el-button>

删除逻辑实现

实现批量删除方法,通常需要调用API接口:

methods: {
  handleBatchDelete() {
    if (this.multipleSelection.length === 0) {
      this.$message.warning('请至少选择一项');
      return;
    }

    this.$confirm('确认删除选中项?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      const ids = this.multipleSelection.map(item => item.id);
      // 调用API
      deleteItems(ids).then(res => {
        this.$message.success('删除成功');
        this.fetchData(); // 重新加载数据
        this.$refs.multipleTable.clearSelection(); // 清空选择
      });
    }).catch(() => {
      this.$message.info('已取消删除');
    });
  }
}

API接口示例

假设后端API接收ID数组进行批量删除:

const deleteItems = (ids) => {
  return axios.delete('/api/items/batch', { data: { ids } });
}

注意事项

  • 确保row-key使用唯一标识字段
  • 批量操作前做好数据校验
  • 删除成功后更新表格数据并清空选择
  • 添加适当的用户反馈(成功/失败提示)

完整组件示例

<template>
  <div>
    <el-button 
      type="danger" 
      :disabled="!multipleSelection.length" 
      @click="handleBatchDelete">
      批量删除
    </el-button>

    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      row-key="id">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <!-- 其他列 -->
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleBatchDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请至少选择一项');
        return;
      }

      this.$confirm('确认删除选中项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = this.multipleSelection.map(item => item.id);
        deleteItems(ids).then(res => {
          this.$message.success('删除成功');
          this.fetchData();
          this.$refs.multipleTable.clearSelection();
        });
      });
    },
    fetchData() {
      // 获取表格数据
    }
  },
  mounted() {
    this.fetchData();
  }
}
</script>

标签: 批量vue
分享给朋友:

相关文章

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 TreeS…