Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import {Student} from '../student';
- import {StudentServiceService} from '../student-service.service';
- import {Sort} from '@angular/material';
- @Component({
- selector: 'app-students',
- templateUrl: './students.component.html',
- styleUrls: ['./students.component.css']
- })
- export class StudentsComponent implements OnInit {
- sortedData: Student[]=[];
- students: Student[]=[];
- valami:String[]=[
- 'kekesz','pizza','anyád','valami'
- ];
- constructor(
- private studentService:StudentServiceService) {}
- ngOnInit() {
- this.studentService.getStudents()
- .subscribe((response)=>{
- this.students = response;
- this.sortedData = this.students.slice();
- });
- }
- /*getStudents() :void {
- this.studentService.getStudents()
- .subscribe(students=>this.students=students);
- }*/
- sortData(sort: Sort) {
- const data = this.students.slice();
- if (!sort.active || sort.direction === '') {
- this.sortedData = data;
- return;
- }
- this.sortedData = data.sort((a, b) => {
- const isAsc = sort.direction === 'asc';
- switch (sort.active) {
- case 'lastName': return compare(a.lastName, b.lastName, isAsc);
- case 'firstName': return compare(a.firstName, b.firstName, isAsc);
- case 'postCode': return compare(a.postCode, b.postCode, isAsc);
- case 'age': return compare(a.age, b.age, isAsc);
- case 'email': return compare(a.email, b.email, isAsc);
- case 'id': return compare(a.id, b.id, isAsc);
- default: return 0;
- }
- });
- }
- }
- function compare(a: number | string, b: number | string, isAsc: boolean) {
- console.log(this.valami.sort(a,b));
- return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement