Advertisement
andreadc

gallery.component.ts

Mar 6th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CarsService } from '../services/cars.service';
  2. import { Car } from './../models/car';
  3. import { Component, OnInit } from '@angular/core';
  4. import { BrandCB } from '../models/brandCB';
  5. import { map, tap } from 'rxjs/operators';
  6. import { BehaviorSubject, Subject } from 'rxjs';
  7.  
  8. @Component({
  9.   selector: 'app-gallery',
  10.   templateUrl: './gallery.component.html',
  11.   styleUrls: ['./gallery.component.css']
  12. })
  13. export class GalleryComponent implements OnInit {
  14.  
  15.   url = 'assets/';
  16.   cars:Car[]; //to refactor in array
  17.   allCars:Car[]; //to refactor in array
  18.   filteredCars:Car[]; //to refactor in array
  19.   brands:BrandCB[] = []; //to refactor in array
  20.  
  21.   provaArrayObs = new BehaviorSubject<Array<string>>([]);
  22.   array = new Array;
  23.   selectedBrand:string;
  24.   checkboxAll:any = {checked:true};
  25.   subscriptionCars;
  26.   subscriptionBrands;
  27.  
  28.   constructor(public carsService: CarsService) {
  29.    
  30.   }
  31.  
  32.   ngOnInit() {
  33.  
  34.     this.getCars();
  35.  
  36.     this.subscriptionBrands = this.carsService.getBrands()
  37.      
  38.       .subscribe( {
  39.         next:(brands:BrandCB[]) => {
  40.           this.brands = brands
  41.         },
  42.         error:error => {
  43.           console.error(error);
  44.         }
  45.  
  46.     });
  47.     //I did a test in this way, is it the right solution?
  48.     for(let j=0;j<10;j++){
  49.       this.array.push('prova' + j);
  50.     }
  51.     this.provaArrayObs.next(this.array);
  52.  
  53.   }
  54.  
  55.   getCars(){
  56.    
  57.     this.subscriptionCars = this.carsService.getCars().subscribe({
  58.       next:data => {
  59.    
  60.         this.allCars = data;
  61.         this.filterCars();
  62.          
  63.       },
  64.  
  65.       error:error => {
  66.         console.error(error);
  67.  
  68.       }
  69.  
  70.     });
  71.  
  72.   }
  73.  
  74.   filterCars(){
  75.     if(!this.checkboxAll.checked){
  76.       // array con solo i brand selezionati e con solo i brand senza altre proprietà
  77.       const checkedBrands = this.brands.filter(brand => brand.checked);
  78.       // filtro i dati dal server prelevando solo quelli che hanno un brand presente in checkedBrands
  79.       this.filteredCars = this.allCars.filter(car => checkedBrands.find(brand => brand.brand === car.brand));
  80.       this.filteredCars.sort((a,b) => a.brand.localeCompare(b.brand));
  81.     } else {
  82.       this.filteredCars = this.allCars;
  83.     }
  84.   }
  85.  
  86.   checkboxChange(event){
  87.    
  88.     let name = event.target.name;
  89.     let checked = event.target.checked;
  90.    
  91.     if(name=='all'){
  92.       if(checked==true){
  93.         for(let i=0;i<this.brands.length;i++){
  94.           this.brands[i].checked = false;
  95.         }
  96.       }
  97.     } else {
  98.       if(checked==true){
  99.         // checkbox all = unchecked
  100.         this.checkboxAll.checked = false;
  101.       } else {
  102.         let isOneChecked:boolean = false;
  103.         for(let i=0;i<this.brands.length;i++){
  104.           if(this.brands[i].checked)
  105.             isOneChecked = true;
  106.         }
  107.         if(!isOneChecked)
  108.           this.checkboxAll.checked = true;
  109.       }
  110.  
  111.     }
  112.  
  113.     this.filterCars();
  114.  
  115.   }
  116.  
  117.   ngOnDestroy(){
  118.     this.subscriptionCars.unsubscribe();
  119.     this.subscriptionBrands.unsubscribe();
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement