Your code is a bit complicated to understand, but I think it will help.
Create a method to return the number of attendances and absences of each student, go through the alunos
object using the forEach
method, every loop make a filter on the presencas
object using the filter
method, it return a new array with the objects that passed the test, then retrieve the number of elements in that array.
calcularPresencas () {
this.alunos.forEach((aluno, index) => {
// Filtra por "cd_aluno" e pela "situacao" para presença
this.alunos[index].totalPresente = this.presencas.filter(p => p.cd_aluno === aluno.cd_aluno && p.situacao === 'p').length;
// Filtra por "cd_aluno" e pela "situacao" para falta
this.alunos[index].totalFalta = this.presencas.filter(p => p.cd_aluno === aluno.cd_aluno && p.situacao === 'f').length;
});
}
Run it on method ngOnInit()
after this.getAlunos();
...
this.calcularPresencas();
Method of salvar(aluno, dia, situacao)
filter on object presencas
by cd_aluno
and nu_dia
, if the number of elements returned is greater than 0 it updates the existing situation, if otherwise it will add the object form
to the object presencas
. Then call the calcularPresencas
method to update the data:
// Filtra por "cd_aluno" e "nu_dia"
let al = this.presencas.filter(p => p.cd_aluno == aluno.cd_aluno && p.nu_dia === dia);
// Verifica se o número de elementos retornado é maior que "0"
(al.length > 0) ? al[0].situacao = situacao : this.presencas.push(form);
// Executa o método para atualizar os dados
this.calcularPresencas();
The template:
<!-- PRECISO SOMAR AS FALTAS -->
<td style="text-align: center">{{aluno.totalFalta}}</td>
<!-- PRECISO SOMAR AS PRESENÇAS -->
<td style="text-align: center">{{aluno.totalPresente}}</td>
Notice that I changed the order because it was wrong.
You can see it working at stackblitz .