el-table动态添加行,删除行
html
<template>
<div class="table-demo">
<el-button type="primary" @click="addRow" style="margin-bottom: 10px;">
添加行
</el-button>
<el-table
:data="tableData"
border
style="width: 100%"
>
<el-table-column prop="name" label="姓名" width="180">
<template #default="scope">
<el-input v-model="scope.row.name" placeholder="请输入姓名"></el-input>
</template>
</el-table-column>
<el-table-column prop="age" label="年龄" width="180">
<template #default="scope">
<el-input v-model.number="scope.row.age" type="number" placeholder="请输入年龄"></el-input>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template #default="scope">
<el-input v-model="scope.row.address" placeholder="请输入地址"></el-input>
</template>
</el-table-column>
<el-table-column label="操作" width="120">
<template #default="scope">
<el-button
type="danger"
size="small"
@click="deleteRow(scope.$index)"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { ElTable, ElTableColumn, ElButton, ElInput } from 'element-plus'
// 表格数据源
const tableData = ref([
{ name: '张三', age: 20, address: '北京市' },
{ name: '李四', age: 25, address: '上海市' }
])
// 添加行
const addRow = () => {
tableData.value.push({})
}
// 删除行
const deleteRow = (index) => {
tableData.value.splice(index, 1)
}
</script>
<style scoped>
.table-demo {
width: 800px;
margin: 20px auto;
padding: 20px;
border: 1px solid #eee;
border-radius: 4px;
}
</style>