Поправил студента

This commit is contained in:
ArtemEmelyanov 2023-05-12 15:04:39 +04:00
parent 51f3480755
commit 6159f006a7
4 changed files with 49 additions and 25 deletions

Binary file not shown.

View File

@ -1,7 +1,7 @@
<template>
<div class="container mt-4">
<h1 class="text-center mb-4">Group Table</h1>
<button class="btn btn-primary mr-2" @click="openModal(); group.status = `create`">Добавить</button>
<button class="btn btn-primary mr-2" @click="openModal(); group = new Group(); group.status = `create`">Добавить</button>
<table class="table table-striped">
<thead>
<tr>
@ -50,6 +50,11 @@ import 'axios';
import axios from "axios";
import Group from "@/models/Group";
export default {
computed: {
Group() {
return Group
}
},
created() {
this.getGroups();
},

View File

@ -1,7 +1,7 @@
<template>
<div class="container mt-4">
<h1 class="text-center mb-4">Student Table</h1>
<button class="btn btn-primary mr-2" @click="openModal(); student.status = `create`">Добавить</button>
<button class="btn btn-primary mr-2" @click="openModal('create')">Добавить</button>
<table class="table table-striped">
<thead>
<tr>
@ -17,7 +17,7 @@
<td>{{ stud.birthDate }}</td>
<td>{{ stud.groupName }}</td>
<td>
<button class="btn btn-primary mr-2" @click="openModal(); student = stud; student.status = `edit`">Изменить</button>
<button class="btn btn-primary mr-2" @click="openModal('edit', stud)">Изменить</button>
<button class="btn btn-danger" @click="deleteStudent(stud.id)">Удалить</button>
</td>
</tr>
@ -35,15 +35,15 @@
<form>
<div class="form-group">
<label for="name">Имя:</label>
<input type="text" class="form-control" id="name" name="name" v-model="student.name">
<input type="text" class="form-control" id="name" name="name" v-model="editedStudent.name">
</div>
<div class="form-group">
<label for="birthdate">Дата рождения:</label>
<input type="date" class="form-control" id="birthdate" name="birthdate" v-model="student.birthDate">
<input type="date" class="form-control" id="birthdate" name="birthdate" v-model="editedStudent.birthDate">
</div>
<div class="form-group">
<label for="group">Группа:</label>
<select v-model="student.groupId" class="form-control">
<select v-model="editedStudent.groupId" class="form-control">
<option v-for="(group, index) in groups" :key="index" :value="group.id">
{{ group.name }}
</option>
@ -53,10 +53,8 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="editModal" @click="closeModal()">Закрыть</button>
<button type="button" class="btn btn-primary" v-if="student.status === `create`"
@click="addStudent">Создать</button>
<button type="button" class="btn btn-primary" v-else
@click="editStudent(student)">Сохранить</button>
<button type="button" class="btn btn-primary" v-if="editedStudent.status === 'create'" @click="addStudent(editedStudent)">Создать</button>
<button type="button" class="btn btn-primary" v-else @click="editStudent(editedStudent)">Сохранить</button>
</div>
</div>
</div>
@ -83,6 +81,7 @@ export default {
groups: [],
URL: "http://localhost:8080/",
student: new Student(),
editedStudent: new Student(),
}
},
methods: {
@ -106,14 +105,15 @@ export default {
console.log(error);
});
},
addStudent(){
console.log(this.student);
axios.post(this.URL + "student", this.student)
addStudent(student) {
console.log(student);
axios
.post(this.URL + "student", student)
.then(() => {
this.getStudents();
this.closeModal();
})
.catch(error => {
.catch((error) => {
console.log(error);
});
},
@ -123,20 +123,34 @@ export default {
this.getStudents();
})
},
editStudent(student){
this.student = student;
axios.put(this.URL + `student/${student.id}`, this.student)
.then(() =>{
this.getStudents();
})
this.closeModal();
},
openModal() {
openModal(status, student = null) {
if (status === "create") {
this.editedStudent = new Student();
this.editedStudent.status = "create";
} else if (status === "edit" && student) {
this.editedStudent = { ...student };
this.editedStudent.status = "edit";
}
document.getElementById("editModal").style.display = "block";
},
closeModal() {
document.getElementById("editModal").style.display = "none";
},
editStudent(student) {
axios.put(this.URL + `student/${student.id}`, student)
.then(() => {
const index = this.students.findIndex((s) => s.id === student.id);
if (index !== -1) {
this.students[index] = { ...student };
}
this.closeModal();
this.getStudents();
})
.catch((error) => {
console.log(error);
});
},
}
}
</script>

View File

@ -1,7 +1,7 @@
<template>
<div class="container mt-4">
<h1 class="text-center mb-4">Subject Table</h1>
<button class="btn btn-primary mr-2" @click="openModal(); subject.status = `create`">Добавить</button>
<button class="btn btn-primary mr-2" @click="openModal(); subject = new Subject(); subject.status = `create`">Добавить</button>
<table class="table table-striped">
<thead>
<tr>
@ -50,6 +50,11 @@ import 'axios';
import axios from "axios";
import Subject from "@/models/Subject";
export default {
computed: {
Subject() {
return Subject
}
},
created() {
this.getSubjects();
},