当前位置:首页 > VUE

vue批量删除实现

2026-03-08 10:04:57VUE

Vue 批量删除实现方法

使用 v-model 绑定选中项

在表格或列表中为每个项目添加复选框,通过 v-model 绑定到数组。选中项的 ID 会存储在数组中。

<template>
  <div>
    <table>
      <tr v-for="item in items" :key="item.id">
        <td><input type="checkbox" v-model="selectedItems" :value="item.id"></td>
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <button @click="batchDelete">批量删除</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    batchDelete() {
      if (this.selectedItems.length === 0) return
      this.items = this.items.filter(item => !this.selectedItems.includes(item.id))
      this.selectedItems = []
    }
  }
}
</script>

结合后端 API 删除

实际项目中通常需要调用 API 进行删除操作。以下示例展示如何结合 axios 发送删除请求。

methods: {
  async batchDelete() {
    if (this.selectedItems.length === 0) return
    try {
      await axios.post('/api/items/batch-delete', { ids: this.selectedItems })
      this.items = this.items.filter(item => !this.selectedItems.includes(item.id))
      this.selectedItems = []
    } catch (error) {
      console.error('删除失败', error)
    }
  }
}

使用 Element UI 实现批量删除

如果使用 Element UI 组件库,可以利用其表格组件内置的多选功能。

<template>
  <el-table
    :data="items"
    @selection-change="handleSelectionChange"
    style="width: 100%">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
  </el-table>
  <el-button @click="batchDelete" :disabled="!selectedItems.length">批量删除</el-button>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      selectedItems: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedItems = val.map(item => item.id)
    },
    batchDelete() {
      this.$confirm('确认删除选中项?', '提示', {
        type: 'warning'
      }).then(async () => {
        await this.$http.post('/api/delete', { ids: this.selectedItems })
        this.fetchData() // 重新获取数据
      })
    }
  }
}
</script>

性能优化建议

对于大数据量场景,建议采用分页删除策略,避免一次性发送过多 ID。可以分批发送删除请求,或让后端支持范围删除。

vue批量删除实现

// 分批删除示例
const batchSize = 50
for (let i = 0; i < this.selectedItems.length; i += batchSize) {
  const batchIds = this.selectedItems.slice(i, i + batchSize)
  await axios.post('/api/batch-delete', { ids: batchIds })
}

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

相关文章

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例: &…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…