import {Component, Input, OnInit} from '@angular/core'; import {FiltersDataService} from '../providers/filters-data.service'; import {FilterInfo} from '../types/filter-info'; import {SelectedFiltersDataService} from '../providers/selected-filters-data.service'; @Component({ selector: 'ics-checkbox', templateUrl: './checkbox.component.html', styleUrls: ['./checkbox.component.scss'] }) export class CheckboxComponent implements OnInit { @Input() filterInfo: FilterInfo; @Input() filterData: any[]; selectedData: any[] = []; selectedAll = true; mData: any[] = []; constructor(private filtersDataService: FiltersDataService, private fs: SelectedFiltersDataService) { } ngOnInit() { this.changeDataOfInput(); } private changeDataOfInput() { for ( let i = 0 ; i < this.filterData.length; i++) { this.mData.push({'name' : this.filterData[i].name, 'value' : this.filterData[i].value, 'selected' : true}); } this.onDataChange(); // this.filterInfo.resolve(true); } selectAll() { for (let i = 0; i < this.mData.length; i++) { this.mData[i].selected = this.selectedAll; } this.onDataChange(); } checkIfAllSelected() { this.selectedAll = this.mData.every(function(item: any) { return item.selected === true; }); this.onDataChange(); } private onDataChange() { const selectedBoxData = []; for (let i = 0; i < this.mData.length; i++) { if (this.mData[i].selected) { selectedBoxData.push(this.mData[i].name); } } if (selectedBoxData.length < 1) { selectedBoxData.push(null); } this.fs.storeSelectedData(this.filterInfo.name, selectedBoxData); } }