fixed and improved sorting for Students table in Admin Profile

main
Alex 5 months ago
parent d26a26f78b
commit 39befe8e9f

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

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

Loading…
Cancel
Save