import { Component, OnInit } from
"@angular/core"
;
import { CourseService } from
"./courseservice"
;
import { LazyLoadEvent, SelectItem } from
"primeng/api"
;
@Component({
selector:
"app-root"
,
templateUrl:
"./app.component.html"
,
styleUrls: [
"./app.component.scss"
]
})
export class AppComponent implements OnInit {
courses: Course[];
virtualCourses: Course[];
sortingKeyGfg: string;
listOfOption: SelectItem[];
constructor(private courseService: CourseService) {}
ngOnInit() {
this
.courses = Array.from({ length: 5000 }).map(() =>
this
.courseService.generateCourse()
);
this
.virtualCourses = Array.from({ length: 5000 });
this
.listOfOption = [
{ label:
"Lowest Price First"
, value:
"price"
},
{ label:
"Highest Price First"
, value:
"!price"
}
];
}
lazyCourse(event: LazyLoadEvent) {
setTimeout(() => {
let loadedProducts =
this
.courses.slice(
event.first,
event.first + event.rows
);
Array.prototype.splice.apply(
this
.virtualCourses, [
...[event.first, event.rows],
...loadedProducts
]);
}, 500);
}
sortingGeekCourses() {
if
(
this
.sortingKeyGfg.indexOf(
"!"
) === 0)
this
.sort(-1);
else
this
.sort(1);
}
sort(order: number): void {
let courses = [...
this
.courses];
courses.sort((data1, data2) => {
let value1 = data1.price;
let value2 = data2.price;
let result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
return
order * result;
});
this
.courses = courses;
}
}
export interface Course {
id?: string;
name?: string;
description?: string;
price?: number;
quantity?: number;
inventoryStatus?: string;
category?: string;
rating?: number;
}