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

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> <template>
<div class="container mt-4"> <div class="container mt-4">
<h1 class="text-center mb-4">Group Table</h1> <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"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
@ -50,6 +50,11 @@ import 'axios';
import axios from "axios"; import axios from "axios";
import Group from "@/models/Group"; import Group from "@/models/Group";
export default { export default {
computed: {
Group() {
return Group
}
},
created() { created() {
this.getGroups(); this.getGroups();
}, },

View File

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

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="container mt-4"> <div class="container mt-4">
<h1 class="text-center mb-4">Subject Table</h1> <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"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
@ -50,6 +50,11 @@ import 'axios';
import axios from "axios"; import axios from "axios";
import Subject from "@/models/Subject"; import Subject from "@/models/Subject";
export default { export default {
computed: {
Subject() {
return Subject
}
},
created() { created() {
this.getSubjects(); this.getSubjects();
}, },