fixed and improved sorting for Students table in Admin Profile

main
Alex 5 months ago
parent d26a26f78b
commit 39befe8e9f

@ -40,9 +40,7 @@
<br>
you can search for specific students
<br>
or filter them how you would like. For searching a specific <span style="color:white;">Student</span>, make sure
<br>
<span style="color:white;">Name</span> is selected in the dropdown
or filter them how you would like
</span>
</div>
<br>

@ -23,7 +23,6 @@ export class AdminProfileComponent implements OnInit{
subs: Submission[] = []
flags: Flag[] = []
selectedContest: string = '';
sortOption: string = '';
constructor(private router: Router, private adminProfileService: AdminProfileService){}
emailToDelete = '';
searchText: string = '';
@ -138,22 +137,18 @@ export class AdminProfileComponent implements OnInit{
this.emailToDelete = '';
}
// sort the students table
sortTable(): void{
// get the name and the option selected
const name = this.searchText.trim().toLowerCase();
const option = this.sortOption;
let sorted = [...this.users];
let optionElement = document.getElementById('Options') as HTMLSelectElement;
let option = optionElement.value;
if((option === 'Name (A->Z)' || option === 'Name (Z->A)') && name !== ''){
const targetIndex = sorted.findIndex(s => s.Name.toLowerCase() === name);
if(targetIndex !== -1){
const student = sorted.splice(targetIndex, 1)[0];
sorted.unshift(student);
}
else{
alert('Student is not in database');
}
}
let sorted = this.users;
// sort the students first
if(option === 'Name (A->Z)'){
sorted.sort((a, b) => a.Name.localeCompare(b.Name));
}
@ -164,8 +159,29 @@ export class AdminProfileComponent implements OnInit{
sorted.sort((a, b) => b.Flags - a.Flags);
}
else if(option === 'Flags (L->H)'){
sorted.sort((b, a) => a.Flags - b.Flags);
sorted.sort((b, a) => b.Flags - a.Flags);
}
// search for specific student
if (name !== '') {
let targetIndex = sorted.findIndex(s => s.Name.toLowerCase() === name);
// if no match of student then look for partial
if (targetIndex === -1) {
targetIndex = sorted.findIndex(s => s.Name.toLowerCase().includes(name));
}
// student in database
if(targetIndex !== -1){
const [student] = sorted.splice(targetIndex, 1);
sorted.unshift(student);
}
else {
alert('Student is not in the database');
}
}
// display sorted array
this.displayedUsers = sorted;
}

Loading…
Cancel
Save