Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import {Student} from '../student';
  3. import {StudentServiceService} from '../student-service.service';
  4. import {Sort} from '@angular/material';
  5.  
  6. @Component({
  7.   selector: 'app-students',
  8.   templateUrl: './students.component.html',
  9.   styleUrls: ['./students.component.css']
  10. })
  11. export class StudentsComponent implements OnInit {
  12.   sortedData: Student[]=[];
  13.   students: Student[]=[];
  14.  
  15.   valami:String[]=[
  16.   'kekesz','pizza','anyád','valami'
  17.   ];
  18.  
  19.   constructor(
  20.     private studentService:StudentServiceService) {}
  21.  
  22.   ngOnInit() {
  23.     this.studentService.getStudents()
  24.     .subscribe((response)=>{
  25.       this.students = response;
  26.      this.sortedData = this.students.slice();
  27.     });
  28.   }
  29.  
  30.   /*getStudents() :void {
  31.     this.studentService.getStudents()
  32.       .subscribe(students=>this.students=students);
  33.   }*/
  34.  
  35. sortData(sort: Sort) {
  36.   const data = this.students.slice();
  37.   if (!sort.active || sort.direction === '') {
  38.     this.sortedData = data;
  39.     return;
  40.   }
  41.  
  42.   this.sortedData = data.sort((a, b) => {
  43.     const isAsc = sort.direction === 'asc';
  44.     switch (sort.active) {
  45.       case 'lastName': return compare(a.lastName, b.lastName, isAsc);
  46.       case 'firstName': return compare(a.firstName, b.firstName, isAsc);
  47.       case 'postCode': return compare(a.postCode, b.postCode, isAsc);
  48.       case 'age': return compare(a.age, b.age, isAsc);
  49.       case 'email': return compare(a.email, b.email, isAsc);
  50.       case 'id': return compare(a.id, b.id, isAsc);
  51.       default: return 0;
  52.     }
  53.   });
  54.  
  55. }
  56. }
  57.  
  58.  function compare(a: number | string, b: number | string, isAsc: boolean) {
  59.  
  60.   console.log(this.valami.sort(a,b));
  61.  
  62.  
  63.     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
  64.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement