Angular 8. Исправление экспорта имени в таблицу сайта
Почти завершил проект, но возникла одна проблема. Форма написана на Angular 8, бакэнд написан на Laravel 6. Как сделать получение имени студента в таблицу, на строке this.students.push(data); но не .push студентов, а экспорт имени из базы по id на основе метода OnInit в этом же коде? Наподобие this students getAll() или onInit(), или this.students=this students ;
this.studentService.getAll(); не работает
import { Component, OnInit } from '@angular/core';
import { first } from 'rxjs/operators';
import { FormBuilder, Validators } from '@angular/forms';
import { User } from '@/_models';
import {AuthenticationService, RatingService, StudentService} from '@/_services';
@Component({ templateUrl: 'ratings.component.html' })
export class RatingComponent implements OnInit {
ratingForm = this.fb.group({
student_id: [ '', Validators.required ],
name: [ '', Validators.required ],
discipline: [ '', Validators.required ],
mark: [ '', Validators.required ],
});
public ratings = [];
public students = [];
currentUser: User;
constructor(
private authenticationService: AuthenticationService,
private ratingService: RatingService,
private studentService: StudentService,
private fb: FormBuilder
) {
this.currentUser = this.authenticationService.currentUserValue;
}
ngOnInit() {
this.ratingService.getAll()
.subscribe(
data => {
this.ratings = data;
},
error => {
console.log(error);
});
this.studentService.getAll()
.subscribe(
data => {
this.students = data;
},
error => {
console.log(error);
});
}
deleteRating(id: number) {
this.ratingService.delete(id)
.pipe(first());
}
onSubmit() {
const rating = {
student_id: this.ratingForm.controls.student_id.value,
name: this.ratingForm.controls.name.value,
discipline: this.ratingForm.controls.discipline.value,
mark: this.ratingForm.controls.mark.value,
};
console.log(rating)
this.ratingService.addRating(rating)
.subscribe(
data => {
this.ratings.push(data);
this.students.push(data);
},
error => {
console.log(error);
});
}
}
На основе ngFor, который в 5 строке в html-форме
<form [formGroup]="ratingForm" (ngSubmit)="onSubmit()" class="">
<a href="http://localhost:8000/rating/export">Скачать рейтинг</a>
<select id="student_id" name="student_id" formControlName="student_id">
<option *ngFor="let student of students" value="{{student.id}}">{{student.name}}</option>
</select>
<!--<div class="form-group row">
<label for="student_id" class="col-sm-2 col-form-label col-form-label-sm">Student_ID</label>
<div class="col-sm-10">
<input type="text" formControlName="student_id" class="form-control form-control-sm" id="student_id" placeholder="student_id">
</div>
</div>-->
<!--<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label col-form-label-sm">Name</label>
<div class="col-sm-10">
<input type="text" formControlName="name" class="form-control form-control-sm" id="name" placeholder="name">
</div>
</div>-->
<div class="form-group row">
<label for="discipline" class="col-sm-2 col-form-label col-form-label-sm">Discipline</label>
<div class="col-sm-10">
<input type="text" formControlName="discipline" class="form-control form-control-sm" id="discipline" placeholder="discipline">
</div>
</div>
<div class="form-group row">
<label for="mark" class="col-sm-2 col-form-label col-form-label-sm">Mark</label>
<div class="col-sm-10">
<input type="text" formControlName="mark" class="form-control form-control-sm" id="mark" placeholder="mark">
</div>
</div>
<input value="Save in XLSX" type="button" onclick="location.href='http://localhost:8000/rating/export'" />
<button type="submit" class="btn btn-primary">Add</button>
<!--<button (click)="onExcel()">Export in Excel</button>-->
<!--<button type="submit" [disabled]="!ratingForm.valid" class="btn btn-primary">Export in Excel</button>-->
</form>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Student_ID</th>
<th scope="col">Name</th>
<th scope="col">Discipline</th>
<th scope="col">Mark</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let rating of ratings">
<th scope="row">{{ rating.id }}</th>
<td>{{ rating.student_id }}</td>
<td>{{ rating.name }}</td>
<td>{{ rating.discipline }}</td>
<td>{{ rating.mark }}</td>
</tr>
</tbody>
</table>
</div>