vue2中el-dialog实现新增el-table行的数据
时间: 2023-07-22 20:17:09 浏览: 78
要在el-dialog中实现新增el-table行的数据,需要进行以下步骤:
1. 在el-dialog中添加表单元素,用于输入新增行的数据。
2. 在表单中添加提交按钮,用于提交新增行的数据。
3. 在点击提交按钮时,将表单数据保存到一个对象中。
4. 将保存的对象添加到el-table的数据源中。
下面是一个简单的示例代码,演示如何在el-dialog中新增el-table行的数据:
```
<template>
<div>
<el-button type="primary" @click="showDialog">新增</el-button>
<el-dialog :visible.sync="dialogVisible">
<el-form ref="form" :model="formData">
<el-form-item label="姓名">
<el-input v-model="formData.name"></el-input>
</el-form-item>
<el-form-item label="年龄">
<el-input v-model.number="formData.age"></el-input>
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="addRow">提交</el-button>
</div>
</el-dialog>
<el-table :data="tableData">
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
dialogVisible: false,
formData: {
name: '',
age: 0
},
tableData: [
{ name: '张三', age: 20 },
{ name: '李四', age: 22 },
{ name: '王五', age: 25 }
]
};
},
methods: {
showDialog() {
this.dialogVisible = true;
},
addRow() {
this.tableData.push(this.formData);
this.dialogVisible = false;
this.$refs.form.resetFields();
}
}
};
</script>
```
在上面的示例中,我们通过el-dialog实现了一个表单,用于输入新增行的数据。当点击提交按钮时,我们将表单数据保存到formData对象中,并将该对象添加到tableData数组中,从而实现了新增el-table行的数据。同时,我们还使用了$refs来重置表单数据,以便下一次新增行的操作。