当前位置:首页 > 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 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现列表

vue实现列表

Vue 实现列表的方法 在 Vue 中实现列表渲染通常使用 v-for 指令,可以动态生成多个元素。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 遍历数组,生成列表项。v-for 需要…

vue实现框选

vue实现框选

Vue 实现框选功能 在 Vue 中实现框选功能通常需要监听鼠标事件,计算选区范围,并根据选区范围高亮或选中元素。以下是实现框选功能的关键步骤。 监听鼠标事件 在 Vue 模板中,为容器元素绑定鼠标…

vue实现左右菜单

vue实现左右菜单

实现左右菜单的基本思路 使用Vue实现左右菜单通常需要两个主要组件:左侧的导航菜单和右侧的内容区域。左侧菜单用于导航,右侧区域根据菜单选择动态展示内容。 创建左侧菜单组件 左侧菜单通常是一个列表,可…

vue实现数据增加

vue实现数据增加

vue实现数据增加的几种方法 在Vue中实现数据增加通常涉及以下几个核心概念和方法: 响应式数据声明 在Vue组件中,使用data选项或ref(Vue 3)声明响应式数据: // Vue…