后端
数据回显,根据ID回显
@GetMapping("/findById/{id}")
public Users findById(@PathVariable("id") Integer id){
return usersRepository.findById(id).get();
}
修改数据,根据回显的数据回显
@PutMapping("/update")
public String update(@RequestBody Users users){
Users result = usersRepository.save(users);
if (result!= null){
return "success";
}else {
return "SB, this is error";
}
}
前端
写一个路由,支持修改跳转
import UserUpdate from "../views/UserUpdate.vue";
{
path: '/userUpate',
component: UserUpdate
}
UserUpdate.vue
<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="ID">
<el-input v-model="ruleForm.id" readOnly></el-input>
</el-form-item>
<el-form-item label="名称" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="ruleForm.password"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="ruleForm.email"></el-input>
</el-form-item>
<el-form-item label="生日" prop="birthday">
<el-input v-model="ruleForm.birthday"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
ruleForm: {
id: '',
name: '',
password: '',
email: '',
birthday: ''
},
rules: {
name: [
{required: true, message: '名称不能为空', trigger: 'blur'},
],
password: [
{required: true, message: '密码不能为空', trigger: 'blur'}
],
email: [
{required: true, message: '邮箱不能为空', trigger: 'blur'},
{type: 'email', message: '邮箱格式不正确', trigger: 'blur'}
],
birthday: [
{required: true, message: '生日不能为空', trigger: 'blur'}
]
}
};
},
methods: {
submitForm(formName) {
const _this = this
this.$refs[formName].validate((valid) => {
if (valid) {
axios.put('http://localhost:8181/user/update',this.ruleForm).then(function (resp){
if (resp.data == 'success'){
alert('修改成功')
}
})
} else {
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
},
created(){
const _this = this
axios.get('http://localhost:8181/user/findById/'+this.$route.query.id).then(function (resp){
_this.ruleForm = resp.data
})
}
}
</script>