diff options
Diffstat (limited to 'ufund-ui/src/app/components/need-list')
-rw-r--r-- | ufund-ui/src/app/components/need-list/need-list.component.html | 5 | ||||
-rw-r--r-- | ufund-ui/src/app/components/need-list/need-list.component.ts | 46 |
2 files changed, 31 insertions, 20 deletions
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.html b/ufund-ui/src/app/components/need-list/need-list.component.html index a653527..5be915c 100644 --- a/ufund-ui/src/app/components/need-list/need-list.component.html +++ b/ufund-ui/src/app/components/need-list/need-list.component.html @@ -3,9 +3,12 @@ <label for="sort">Sort by:</label> <select [(ngModel)]="sortSelection" id="sort" (change)="changeSortAlgo(sortSelection, searchForm.value)"> <option *ngFor="let algorithm of SortingAlgoArrays" value="{{algorithm.name}}"> - {{algorithm.display}} + {{algorithm.display[sortMode === 'Ascending' ? 0 : 1]}} </option> </select> + <button (click)="changeSortMode(searchForm.value)"> + {{sortMode}} + </button> <form id="search-form" #searchForm="ngForm"> <input type="text" name="search" placeholder="Search in {{needs.length}} needs..." (input)="search(searchForm.value)" ngModel> <input type="reset" value="Clear" (click)="search(null)"> <br> diff --git a/ufund-ui/src/app/components/need-list/need-list.component.ts b/ufund-ui/src/app/components/need-list/need-list.component.ts index 2fdd993..97be0cb 100644 --- a/ufund-ui/src/app/components/need-list/need-list.component.ts +++ b/ufund-ui/src/app/components/need-list/need-list.component.ts @@ -17,11 +17,7 @@ const sortByName: sortAlgo = (a: Need, b: Need): number => { return 1; } -const sortByNameReverse: sortAlgo = (a: Need, b: Need): number => { - return sortByName(a,b)*-1; -} - -const sortByMaxGoal: sortAlgo = (a: Need, b: Need): number => { +const sortByGoal: sortAlgo = (a: Need, b: Need): number => { if(a.maxGoal == b.maxGoal) { return sortByName(a,b); } @@ -31,10 +27,6 @@ const sortByMaxGoal: sortAlgo = (a: Need, b: Need): number => { return 1; } -const sortByMinGoal: sortAlgo = (a: Need, b: Need): number => { - return sortByMaxGoal(a,b)*-1; -} - @Component({ selector: 'app-need-list', standalone: false, @@ -45,14 +37,13 @@ export class NeedListComponent { selectedNeed: Need | null = null; needs: Need[] = []; searchResults: Need[] = []; + sortMode = 'Ascending' sortSelection: string = ''; - currentSortAlgo: sortAlgo = sortByMaxGoal; + currentSortAlgo: sortAlgo = sortByGoal; - SortingAlgoArrays: {func:sortAlgo,name:string,display:string}[] = [ - {func:sortByMaxGoal,name:"sortByMaxGoal", display:"Max Goal"}, - {func:sortByName,name:"sortByName", display:"Name"}, - {func:sortByNameReverse,name:"sortByNameReverse", display:"Name (reverse)"}, - {func:sortByMinGoal,name:"sortByMinGoal", display:"Min Goal"}, + SortingAlgoArrays: {func:sortAlgo,name:string, display:string[]}[] = [ + {func:sortByGoal,name:"sortByMaxGoal", display:["Max Goal", "Min Goal"]}, + {func:sortByName,name:"sortByName", display:["Name", "Name (Descending)"]}, ]; @Output() currentNeed = new EventEmitter<Need>(); @@ -64,22 +55,34 @@ export class NeedListComponent { refresh() { this.cupboardService.getNeeds().subscribe(n => { - this.needs = n.sort(this.currentSortAlgo); + if (this.sortMode == 'Ascending') { + this.needs = n.sort(this.currentSortAlgo); + } else { + this.needs = n.sort(this.currentSortAlgo).reverse(); + } this.searchResults = this.needs; }); - console.log(this.searchResults); } ngOnInit(): void { this.refresh() } + changeSortMode(form : any) { + if (this.sortMode == 'Ascending'){ + this.sortMode = 'Descending' + } else { + this.sortMode = 'Ascending' + } + this.search(form) + } + changeSortAlgo(algoName: string, form: any) { console.log(algoName); this.SortingAlgoArrays.forEach(algo => { if(algo.name === algoName) { this.currentSortAlgo = algo.func; - console.log("changed sorting algorithm to: ", algo.name) + console.log("changed sorting algorithm to: ", algo.name + this.sortMode) return } }); @@ -102,7 +105,12 @@ export class NeedListComponent { if (form) { const currentSearchValue = form.search; //latest value of the search this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => { - this.searchResults = n.sort(this.currentSortAlgo); + if (this.sortMode == 'Ascending') { + this.searchResults = n.sort(this.currentSortAlgo); + } else { + this.searchResults = n.sort(this.currentSortAlgo).reverse(); + } + console.log(currentSearchValue, this.searchResults); }); } |