当前位置:首页 > 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框架

如果使用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交互

实际项目中通常需要调用后端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)
    }
  }
}

添加确认提示

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

vue实现多项删除

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

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

相关文章

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现treeselect

vue实现treeselect

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

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依赖…