elementui的table删除行
时间: 2023-11-29 20:46:26 浏览: 107
jQuery删除table指定行
以下是使用ElementUI的Table组件进行删除行的示例代码:
1. 单独删除行
```html
<template>
<el-table :data="tableData" @row-click="handleRowClick">
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="danger" icon="el-icon-delete" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 18 },
{ name: '李四', age: 20 },
{ name: '王五', age: 22 }
]
}
},
methods: {
handleDelete(index, row) {
this.$confirm(`确定删除 ${row.name} 吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.tableData.splice(index, 1)
this.$message({
type: 'success',
message: '删除成功!'
})
}).catch(() => {})
}
}
}
</script>
```
2. 批量删除行
```html
<template>
<el-table :data="tableData" @row-click="handleRowClick">
<el-table-column type="selection"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="danger" icon="el-icon-delete" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-button type="danger" icon="el-icon-delete" size="small" @click="handleBatchDelete">批量删除</el-button>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 18 },
{ name: '李四', age: 20 },
{ name: '王五', age: 22 }
],
multipleSelection: []
}
},
methods: {
handleDelete(index, row) {
this.$confirm(`确定删除 ${row.name} 吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.tableData.splice(index, 1)
this.$message({
type: 'success',
message: '删除成功!'
})
}).catch(() => {})
},
handleBatchDelete() {
if (this.multipleSelection.length === 0) {
this.$message({
type: 'warning',
message: '请选择要删除的行!'
})
return
}
this.$confirm(`确定删除选中的 ${this.multipleSelection.length} 行吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.multipleSelection.forEach(item => {
const index = this.tableData.indexOf(item)
this.tableData.splice(index, 1)
})
this.$message({
type: 'success',
message: '删除成功!'
})
this.multipleSelection = []
}).catch(() => {})
}
}
}
</script>
```
阅读全文