Advertisement
andreadc

gallery.component.ts

Feb 18th, 2020
293
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.  
  6. @Component({
  7.   selector: 'app-gallery',
  8.   templateUrl: './gallery.component.html',
  9.   styleUrls: ['./gallery.component.css']
  10. })
  11. export class GalleryComponent implements OnInit {
  12.  
  13.   cars:Car[];
  14.   brands:BrandCB[] = [];
  15.   selectedBrand:string;
  16.   checkboxAll:any = {checked:true};
  17.   observableCars;
  18.   observableBrands;
  19.  
  20.   constructor(public carsService: CarsService) {
  21.    
  22.   }
  23.  
  24.   ngOnInit() {
  25.     this.getCars();
  26.     this.observableBrands = this.carsService.getBrands().subscribe( (data:any) => {
  27.       for(let i=0;i<data.length;i++){
  28.         this.brands.push(new BrandCB(data[i].brand, false));
  29.       }
  30.     });
  31.   }
  32.  
  33.   getCars(){
  34.    
  35.     this.observableCars = this.carsService.getCars().subscribe(data => {
  36.    
  37.       this.cars = [];
  38.       // checkboxAll è la checkbox che permette di prendere tutti gli elementi dell array
  39.       if(!this.checkboxAll.checked){
  40.         // array con solo i brand selezionati e con solo i brand senza altre proprietà
  41.         const checkedBrands = this.brands.filter(brand => brand.checked);
  42.         // filtro i dati dal server prelevando solo quelli che hanno un brand presente in checkedBrands
  43.         this.cars = data.filter(car => checkedBrands.find(brand => brand.brand === car.brand));
  44.         this.cars.sort((a,b) => a.brand.localeCompare(b.brand));
  45.       } else {
  46.         this.cars = data;
  47.       }
  48.  
  49.     });
  50.   }
  51.  
  52.   checkboxChange(event){
  53.     let name = event.target.name;
  54.     let checked = event.target.checked;
  55.    
  56.     if(name=='all'){
  57.       if(checked==true){
  58.         for(let i=0;i<this.brands.length;i++){
  59.           this.brands[i].checked = false;
  60.         }
  61.       }
  62.     } else {
  63.       if(checked==true){
  64.         // checkbox all = unchecked
  65.         this.checkboxAll.checked = false;
  66.       } else {
  67.         let isOneChecked:boolean = false;
  68.         for(let i=0;i<this.brands.length;i++){
  69.           if(this.brands[i].checked)
  70.             isOneChecked = true;
  71.         }
  72.         if(!isOneChecked)
  73.           this.checkboxAll.checked = true;
  74.       }
  75.  
  76.     }
  77.  
  78.     this.getCars();
  79.  
  80.   }
  81.  
  82.   ngOnDestroy(){
  83.     this.observableCars.unsubscribe();
  84.     this.observableBrands.unsubscribe();
  85.   }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement