I need your help
- Input field where I can enter a Student email or IAM, which will be added to a string array
- List displaying all the students I have added, using a for loop as shown below
- Delete button to remove a student from the array
The list has a specified maximum height, and I want to implement a feature that checks if the content exceeds this height. If it does, display a "read more" button; otherwise, hide it.
While the initial implementation works fine, I am facing an issue. When the number of students added causes the total height to exceed the maximum limit, the "read more" button appears correctly. However, upon deleting some students and bringing the total height back under the limit, an error occurs.
I prefer not to use setTimeout unless absolutely necessary
<div class="input-row">
<input #studentsInput
(keypress)="onKeyPressStudent($event, studentsInput)"
class="input-field"
placeholder="Student Email or IAM"
required
type="text">
</div>
<ul #listStudents
[ngClass]="{ 'scrollable': studentsScroll }"
class="list-row">
<li *ngFor="let student of students; index as i"
class="list-item">
{{ student }}
<span (click)="deleteStudent(i)"
class="material-icons">
close
</span>
</li>
</ul>
<div *ngIf="listStudents.offsetHeight < listStudents.scrollHeight"
class="more">
<span class="material-icons">
more_horiz
</span>
</div>
export class Component implements OnInit {
public students: string[] = [];
public studentsScroll = false;
public ngOnInit(): void {
}
public onKeyPressStudent(
event: KeyboardEvent,
studentsInput: HTMLInputElement,
): void {
if (event.key === 'Enter') {
if (studentsInput.checkValidity()) {
this.students.push(studentsInput.value);
studentsInput.value = '';
} else {
this.toastrService.error('Please enter a valid Email Address!');
}
}
}
public deleteStudent(index: number): void {
this.students.splice(index, 1);
}
public toggleStudentScroll(): void {
this.studentsScroll = !this.studentsScroll;
}
}