Last active
March 2, 2018 18:03
-
-
Save rolandoesc/d46fd0aab0a61648bb408047653598e5 to your computer and use it in GitHub Desktop.
ngFor doesn't work when at least 1 pipe is added ; Firebase is used in this case.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform, ContentChild } from '@angular/core'; | |
@Pipe({ | |
name: 'checkboxFilter' | |
}) | |
export class CheckFilter implements PipeTransform { | |
transform(items: any[], filter: any, filterItems: Array < any >, isAnd: boolean): any { | |
console.log('Filtering ..'); | |
if (filter && Array.isArray(items) && filterItems) { | |
let filterKeys = Object.keys(filter); | |
let checkedItems = filterItems.filter(item => { return item.checked; }); | |
if (!checkedItems || checkedItems.length === 0) { return items; } | |
if (isAnd) { | |
return items.filter(item => | |
filterKeys.reduce((acc1, keyName) => | |
(acc1 && checkedItems.reduce((acc2, checkedItem) => acc2 && new RegExp(item[keyName], 'gi').test(checkedItem.value) || checkedItem.value === "", true)) | |
, true) | |
); | |
} else { | |
return items.filter(item => { | |
return filterKeys.some((keyName) => { | |
return checkedItems.some((checkedItem) => { | |
return new RegExp(item[keyName], 'gi').test(checkedItem.value) || checkedItem.value === ""; | |
}); | |
}); | |
}); | |
} | |
} else { | |
return items; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform, ContentChild } from '@angular/core'; | |
@Pipe ({ | |
name: 'wordFilter', | |
}) | |
export class FilterPipe implements PipeTransform { | |
transform(value, ...args) { | |
let resultArray = []; | |
if (args.length === 0) { | |
resultArray = value; | |
} else { | |
for (const item of value) { | |
if (item.Candidato != null && item.Candidato.match(new RegExp('' + args, 'i')) || item.Text.match(new RegExp('' + args, 'i')) | |
|| item.Headline.match(new RegExp('' + args, 'i'))) { | |
resultArray.push(item); | |
} | |
} | |
} | |
return resultArray; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container-fluid linea pad-der"> | |
<h1>Conoce las Propuestas</h1> | |
<div class="col-1"> | |
<hr> | |
</div> | |
<h3>Selecciona tus opciones</h3> | |
<div class='row'> | |
<div class="col-lg-3"> | |
<h5>Candidatos</h5> | |
<label for="" *ngFor='let filterItem of filterItems | slice: 0:3; let i = index'> | |
<input class='candidato' type="checkbox" name="{{ filterItem.category }}" [value]="filterItem.value" [(ngModel)]="filterItem.checked">{{ filterItem.candidate }} | |
</label> | |
<h5>Categoría</h5> | |
<label for="" *ngFor='let filterItem of filterItems | slice: 3:7'> | |
<input class='categoria' type="checkbox" name="{{ filterItem.category }}" [value]="filterItem.value" [(ngModel)]="filterItem.checked">{{ filterItem.value }} | |
<br> | |
</label> | |
<h5>Temática</h5> | |
<label for="" *ngFor='let filterItem of filterItems | slice: 7:17'> | |
<input class='tema' type="checkbox" name="{{ filterItem.category }}" [value]="filterItem.value" [(ngModel)]="filterItem.checked">{{ filterItem.value }} | |
<br> | |
</label> | |
</div> | |
<div class="col-9"> | |
<input type="text" class="" [(ngModel)]='textSearch'> | |
<div id="proposals"> | |
<div class="search-results scroll" infiniteScroll [infiniteScrollDistance]="1" [infiniteScrollUpDistance]="scrollUpDistance" | |
[infiniteScrollThrottle]="throttle" [scrollWindow]="false"> | |
<table class="" id='appending'> | |
<tr *ngFor="let proposal of proposals | wordFilter : textSearch"> | |
<!-- checkboxFilter : checked() | wordFilter : textSearch | --> | |
<td class="candidato"> {{proposal.candidato}} </td> | |
<td class="categoria"> {{proposal.group}} </td> | |
<td class="tema tema-space"> {{ proposal.tema}} </td> | |
<td class="headline">{{ proposal.text.headline}} </td> | |
<td class="text"> {{ proposal.text.text}} </td> | |
<td class="fuente"> | |
<a href="{{ proposal.media.url}}"> Leer nota </a> | |
</td> | |
</tr> | |
</table> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, ViewEncapsulation, AfterViewInit } from '@angular/core'; | |
import { FormsModule } from '@angular/forms'; | |
import { Http, Response } from '@angular/http'; | |
import { HttpModule } from '@angular/http'; | |
import { InfiniteScrollModule } from 'ngx-infinite-scroll'; | |
import { AngularFireDatabase } from 'angularfire2/database'; | |
import { Observable } from '@firebase/util'; | |
@Component({ | |
selector: 'app-filtrotimeline', | |
templateUrl: './filtrotimeline.component.html', | |
styleUrls: ['./filtrotimeline.component.css'], | |
encapsulation: ViewEncapsulation.None, | |
}) | |
export class FiltrotimelineComponent implements OnInit, AfterViewInit { | |
events: any; | |
proposals: any = []; | |
originalProposals: any = []; | |
filteredProposals: any = []; | |
search = ''; | |
constructor(private db: AngularFireDatabase) { | |
} | |
objectKeys = Object.keys; | |
filterItems: Array<any> = [ | |
{ candidate: 'Meade', value: 'JAMK', checked: false }, | |
{ candidate: 'Anaya', value: 'RAC', checked: false }, | |
{ candidate: 'AMLO', value: 'AMLO', checked: false }, | |
{ category: 'Propuestas', value: 'Propuestas', checked: false }, | |
{ category: 'Campaña', value: 'Campaña', checked: false }, | |
{ category: 'Actores', value: 'Actores', checked: false }, | |
{ category: 'Entorno', value: 'Entorno', checked: false }, | |
{ theme: 'Administración Pública', value: 'Administración Pública', checked: false }, | |
{ theme: 'Corrupción', value: 'Corrupción', checked: false }, | |
{ theme: 'Desarrollo Económico', value: 'Desarrollo Económico', checked: false }, | |
{ theme: 'Educación', value: 'Educación', checked: false }, | |
{ theme: 'Género', value: 'Género', checked: false }, | |
{ theme: 'Medio Ambiente', value: 'Medio Ambiente', checked: false }, | |
{ theme: 'Política Exterior', value: 'Política Exterior', checked: false }, | |
{ theme: 'Salud', value: 'Salud', checked: false }, | |
{ theme: 'Seguridad', value: 'Seguridad', checked: false }, | |
]; | |
onScroll() { | |
if (this.proposals.length < this.originalProposals.length) { | |
let len = this.proposals.length; | |
for ( let i = len; i <= len + 20; i++) { | |
this.proposals.push(this.originalProposals[i]); | |
} | |
} | |
} | |
checked() { | |
return this.filterItems.filter(item => item.checked); | |
} | |
searchWord(event: any) { | |
console.log(event.target.value); | |
let resultArray = []; | |
this.search = event.target.value; | |
if (this.search.length === 0) { | |
resultArray = this.originalProposals; | |
} else { | |
for (const word of this.originalProposals) { | |
if (word.Candidato != null && word.Candidato.match(new RegExp('' + this.search, 'i')) | |
|| word.Text.match(new RegExp('' + this.search, 'i')) | |
|| word.Headline.match(new RegExp('' + this.search, 'i'))) { | |
resultArray.push(word); | |
} | |
} | |
} | |
this.proposals = resultArray.slice(0, 20); | |
return [this.proposals, this.search]; | |
} | |
ngOnInit() { | |
this.db.object('data').valueChanges() | |
.subscribe( response => {this.events = response; this.getData(); } ); | |
} | |
ngAfterViewInit() { | |
this.toFilter(); | |
} | |
getData() { | |
for (let index in this.events['events']) { | |
this.proposals.push(this.events['events'][index]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment