当前位置:首页 > VUE

vue实现多项删除

2026-02-19 09:04:53VUE

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="deleteSelected">删除选中项</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' }
      ],
      selectedItems: []
    }
  },
  methods: {
    deleteSelected() {
      this.items = this.items.filter(item => !this.selectedItems.includes(item.id))
      this.selectedItems = []
    }
  }
}
</script>

使用Element UI等UI框架

vue实现多项删除

如果使用Element UI,可以利用其表格组件的多选功能实现更完善的多项删除。

<template>
  <el-table
    ref="multipleTable"
    :data="tableData"
    @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
  </el-table>
  <el-button @click="deleteSelected">批量删除</el-button>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' }
      ],
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteSelected() {
      const ids = this.multipleSelection.map(item => item.id)
      this.tableData = this.tableData.filter(item => !ids.includes(item.id))
    }
  }
}
</script>

与后端API交互

vue实现多项删除

实际项目中通常需要调用后端API删除数据,使用axios发送删除请求。

methods: {
  async deleteSelected() {
    const ids = this.selectedItems
    try {
      await this.$http.delete('/api/items', { data: { ids } })
      this.items = this.items.filter(item => !ids.includes(item.id))
      this.selectedItems = []
    } catch (error) {
      console.error('删除失败', error)
    }
  }
}

添加确认提示

为避免误操作,可以添加确认对话框。

methods: {
  deleteSelected() {
    this.$confirm('确定要删除选中项吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // 删除逻辑
    }).catch(() => {
      this.$message.info('已取消删除')
    })
  }
}

标签: 多项vue
分享给朋友:

相关文章

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <i…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现pdf

vue 实现pdf

在Vue中实现PDF功能 在Vue项目中实现PDF功能通常涉及PDF生成、预览或下载。以下是几种常见实现方式: 使用vue-pdf库预览PDF 安装依赖: npm install vue-pdf…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…