el-table删除当前行
时间: 2023-08-08 15:04:22 浏览: 123
在 Element UI 的 el-table 中删除当前行可以通过以下步骤实现:
1. 获取当前行的索引
```
handleDelete(row) {
const index = this.tableData.indexOf(row);
// ...
}
```
2. 在表格数据中删除该行
```
handleDelete(row) {
const index = this.tableData.indexOf(row);
this.tableData.splice(index, 1);
}
```
3. 刷新表格
```
handleDelete(row) {
const index = this.tableData.indexOf(row);
this.tableData.splice(index, 1);
this.$nextTick(() => {
this.$refs.table.clearSelection();
});
}
```
完整的代码示例:
```
<template>
<div>
<el-table :data="tableData" ref="table">
<!-- 表格列 -->
<el-table-column label="姓名" prop="name"></el-table-column>
<el-table-column label="年龄" prop="age"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 18 },
{ name: '李四', age: 20 },
{ name: '王五', age: 22 },
],
};
},
methods: {
handleDelete(row) {
const index = this.tableData.indexOf(row);
this.tableData.splice(index, 1);
this.$nextTick(() => {
this.$refs.table.clearSelection();
});
},
},
};
</script>
```
阅读全文