提供用vue+element写一个增删改查页面的代码
时间: 2024-03-21 09:39:27 浏览: 52
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
5星 · 资源好评率100%
好的,以下是一个简单的用`Vue`和`Element UI`实现的增删改查页面的代码:
```html
<template>
<div>
<el-form :model="form" :rules="rules" ref="form" label-width="100px">
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model.number="form.age"></el-input>
</el-form-item>
<el-form-item label="性别" prop="gender">
<el-radio-group v-model="form.gender">
<el-radio :label="1">男</el-radio>
<el-radio :label="2">女</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="form.address"></el-input>
</el-form-item>
<el-form-item label="电话" prop="phone">
<el-input v-model.number="form.phone"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" @click="submitForm">提交</el-button>
<el-button type="danger" @click="resetForm">重置</el-button>
<el-table :data="tableData" style="width: 100%">
<el-table-column label="姓名" prop="name"></el-table-column>
<el-table-column label="年龄" prop="age"></el-table-column>
<el-table-column label="性别" prop="gender"></el-table-column>
<el-table-column label="地址" prop="address"></el-table-column>
<el-table-column label="电话" prop="phone"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="editItem(scope.$index, scope.row)">编辑</el-button>
<el-button type="danger" size="mini" @click="deleteItem(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog :visible.sync="dialogVisible" title="编辑" :close-on-click-modal="false">
<el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px">
<el-form-item label="姓名" prop="name">
<el-input v-model="editForm.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model.number="editForm.age"></el-input>
</el-form-item>
<el-form-item label="性别" prop="gender">
<el-radio-group v-model="editForm.gender">
<el-radio :label="1">男</el-radio>
<el-radio :label="2">女</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="editForm.address"></el-input>
</el-form-item>
<el-form-item label="电话" prop="phone">
<el-input v-model.number="editForm.phone"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="updateItem">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
form: {
name: '',
age: '',
gender: '',
address: '',
phone: '',
},
rules: {
name: [
{ required: true, message: '请输入姓名', trigger: 'blur' },
{ min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
],
age: [
{ required: true, message: '请输入年龄', trigger: 'blur' },
{ type: 'number', message: '年龄必须为数字', trigger: 'blur' }
],
gender: [
{ required: true, message: '请选择性别', trigger: 'change' }
],
address: [
{ required: true, message: '请输入地址', trigger: 'blur' }
],
phone: [
{ required: true, message: '请输入电话', trigger: 'blur' },
{ type: 'number', message: '电话必须为数字', trigger: 'blur' }
]
},
tableData: [
{
name: '张三',
age: 18,
gender: '男',
address: '北京市',
phone: 1234567890
},
{
name: '李四',
age: 20,
gender: '女',
address: '上海市',
phone: 1357924680
}
],
dialogVisible: false,
editForm: {
name: '',
age: '',
gender: '',
address: '',
phone: '',
},
editIndex: -1
}
},
methods: {
submitForm() {
this.$refs.form.validate((valid) => {
if (valid) {
this.tableData.push(this.form);
this.$message({
message: '提交成功',
type: 'success'
});
this.resetForm();
} else {
this.$message.error('表单验证失败!');
}
});
},
resetForm() {
this.$refs.form.resetFields();
},
editItem(index, row) {
this.editIndex = index;
this.editForm = Object.assign({}, row);
this.dialogVisible = true;
},
updateItem() {
this.$refs.editForm.validate((valid) => {
if (valid) {
this.tableData.splice(this.editIndex, 1, this.editForm);
this.dialogVisible = false;
this.$message({
message: '修改成功',
type: 'success'
});
} else {
this.$message.error('表单验证失败!');
}
});
},
deleteItem(index, row) {
this.$confirm(`确定删除 ${row.name} 吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.tableData.splice(index, 1);
this.$message({
message: '删除成功',
type: 'success'
});
}).catch(() => {
// 取消删除操作
});
}
}
}
</script>
```
以上代码实现了一个简单的增删改查页面,包括表单验证、表格展示、弹窗编辑等功能。你可以根据需要修改代码,适应自己的业务需求。
阅读全文