当前位置:   article > 正文

【批量删除】Vue3.2 后台管理系统实现批量删除的功能_vue3 多选删除

vue3 多选删除

一、页面表格布局

  1. <el-table ref="multipleTableRef" :data="tableData" border style="width: 100%"
  2. @selection-change="handleSelectionChange" @current-change="CurrentChange"
  3. :header-cell-style="{ background: '#23cdab', color: '#606266' }">
  4. <el-table-column type="selection" width="55" />
  5. <el-table-column prop="name" label="活动名称" />
  6. <el-table-column prop="type" label="活动类型" />
  7. <el-table-column prop="belong" label="活动所属社团" />
  8. <el-table-column prop="number" label="活动人数" />
  9. <el-table-column prop="credit" label="活动学分" />
  10. <el-table-column prop="list" label="人员名单" />
  11. <el-table-column prop="operate" label="操作" width="300">
  12. <template #default="scope">
  13. <el-button color="#23cdab" :icon="Edit" class="editBtn" @click="editBtn">{{ scope.row.operate.edit
  14. }}</el-button>
  15. <el-button type="danger" :icon="Delete" class="delBtn" @click="delBtn">{{ scope.row.operate.del
  16. }}</el-button>
  17. <el-button type="primary" :icon="Setting" class="showBtn" @click="showBtn">{{ scope.row.operate.show
  18. }}</el-button>
  19. </template>
  20. </el-table-column>
  21. </el-table>

二、批量删除按钮

未选择的时候按钮置灰

  1. <el-button type="danger" :icon="Delete" @click="batchDeletion()"
  2. :disabled="batchDel.length === 0">批量删除</el-button>

三、功能模块逻辑

  1. <script setup >
  2. //表格数据
  3. const tableData = ref([
  4. {
  5. id: 1,
  6. name: '11社团',
  7. type: 'IT学习',
  8. belong: '计算机学院',
  9. number: '200',
  10. credit: '3',
  11. list: '安卓开发',
  12. operate: { edit: '编辑', del: '删除', show: '查看' },
  13. },
  14. {
  15. id: 2,
  16. name: '大数据社团',
  17. type: 'IT学习',
  18. belong: '计算机学院',
  19. number: '50',
  20. credit: '3',
  21. list: '互联网',
  22. operate: { edit: '编辑', del: '删除', show: '查看' },
  23. },
  24. {
  25. id: 3,
  26. name: '羽毛球社团',
  27. type: '运动',
  28. belong: '旅游学院',
  29. number: '99',
  30. credit: '2',
  31. list: '软件开发',
  32. operate: { edit: '编辑', del: '删除', show: '查看' },
  33. },
  34. {
  35. id: 4,
  36. name: '排球社团',
  37. type: '运动',
  38. belong: '机电学院',
  39. number: '20',
  40. credit: '2',
  41. list: '空乘',
  42. operate: { edit: '编辑', del: '删除', show: '查看' },
  43. },
  44. {
  45. id: 5,
  46. name: '篮球社团',
  47. type: '运动',
  48. belong: '机电学院',
  49. number: '50',
  50. credit: '2',
  51. list: '互联网',
  52. operate: { edit: '编辑', del: '删除', show: '查看' },
  53. },
  54. {
  55. id: 6,
  56. name: '未来社团',
  57. type: '运动',
  58. belong: '电信学院',
  59. number: '50',
  60. credit: '2',
  61. list: '软件开发',
  62. operate: { edit: '编辑', del: '删除', show: '查看' },
  63. },
  64. {
  65. id: 7,
  66. name: '羽毛球社团',
  67. type: '运动',
  68. belong: '旅游学院',
  69. number: '99',
  70. credit: '2',
  71. list: '软件开发',
  72. operate: { edit: '编辑', del: '删除', show: '查看' },
  73. },
  74. {
  75. id: 8,
  76. name: '排球社团',
  77. type: '运动',
  78. belong: '机电学院',
  79. number: '20',
  80. credit: '2',
  81. list: '空乘',
  82. operate: { edit: '编辑', del: '删除', show: '查看' },
  83. },
  84. {
  85. id: 9,
  86. name: '篮球社团',
  87. type: '运动',
  88. belong: '机电学院',
  89. number: '50',
  90. credit: '2',
  91. list: '互联网',
  92. operate: { edit: '编辑', del: '删除', show: '查看' },
  93. },
  94. {
  95. id: 10,
  96. name: '未来社团',
  97. type: '运动',
  98. belong: '电信学院',
  99. number: '50',
  100. credit: '2',
  101. list: '软件开发',
  102. operate: { edit: '编辑', del: '删除', show: '查看' },
  103. },
  104. ])
  105. const batchDel = ref([])
  106. //指向DOM元素
  107. const multipleTableRef = ref('')
  108. //获取当前选中的数据给到数据
  109. const handleSelectionChange = (val) => {
  110. batchDel.value = []
  111. val.forEach(item => {
  112. batchDel.value.push(item.id)
  113. });
  114. }
  115. const delBtn = (id) => {
  116. ElMessageBox.confirm('此操作将永久删除数据, 是否继续?', '提示', {
  117. confirmButtonText: '确认',
  118. cancelButtonText: '取消',
  119. type: 'warning',
  120. })
  121. .then(() => {
  122. const index = tableData.value.findIndex((item) => item.id === id)
  123. tableData.value.splice(index, 1)
  124. ElMessage({ type: 'success', message: '删除成功' })
  125. })
  126. .catch(() => {
  127. ElMessage({ type: 'info', message: '取消删除' })
  128. })
  129. }
  130. //批量删除
  131. const batchDeletion = () => {
  132. ElMessageBox.confirm("此操作将永久删除数据, 是否继续?", "提示", {
  133. confirmButtonText: "确认",
  134. cancelButtonText: "取消",
  135. type: "warning",
  136. }).then(() => {
  137. batchDel.value.forEach(id => {
  138. const index = tableData.value.findIndex((item) => item.id === id)
  139. tableData.value.splice(index, 1)
  140. })
  141. batchDel.value = []
  142. ElMessage({ type: 'success', message: '删除成功', })
  143. multipleTableRef.value.clearSelection();
  144. }).catch(() => {
  145. ElMessage({ type: 'info', message: '取消删除', })
  146. });
  147. };
  148. <script>

四、效果展示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/100859
推荐阅读
相关标签
  

闽ICP备14008679号