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[]=[
- {id:1,firstName:'Nagy',lastName:'Árpád',email:'sfgsdgfs',age:12,postCode:1100},
- {id:1,firstName:'Kis',lastName:'Árpád',email:'sfgsdgfs',age:22,postCode:1103},
- {id:1,firstName:'Halász',lastName:'Árpád',email:'sfgsdgfs',age:15,postCode:1200},
- {id:1,firstName:'Makkos',lastName:'Árpád',email:'sfgsdgfs',age:32,postCode:1100},
- {id:1,firstName:'Sári',lastName:'Árpád',email:'sfgsdgfs',age:3,postCode:1000},
- {id:1,firstName:'Nagy',lastName:'Árpád',email:'sfgsdgfs',age:88,postCode:1101},
- ];*/
- students: Student[];
- constructor(
- private studentService:StudentServiceService)
- {
- this.sortedData = this.students.slice();
- }
- ngOnInit() {
- // this.getStudents();
- }
- 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);
- default: return 0;
- }
- });
- }
- }
- function compare(a: number | string, b: number | string, isAsc: boolean) {
- return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement