ion3-list
Version:
Ionic 3 template list with filtereing, dynamic loading dom
124 lines (99 loc) • 2.66 kB
text/typescript
import { Component, ContentChild, TemplateRef, Input, Output, EventEmitter, OnChanges } from '@angular/core'
import { FilterByPipe } from 'ngx-pipes'
export class IonTempList implements OnChanges {
template: TemplateRef<any>;
items = []
loaded = false
noDataText = "No data available"
filter: string
filterKeys: Array<string> = []
orderBy: string
limit: number = 15
limitCount: number
loadAjax: EventEmitter<any> = new EventEmitter<any>()
infiniteScroll
areAjaxDataAllLoaded = false
constructor(
public searchPipe: FilterByPipe
) {}
ngOnInit(){
this.limitCount = this.limit
}
ngOnChanges() {
if (!this.isAjaxActive()){
this.limitCount = this.limit
}
if(this.filterKeys.length === 0 && this.items.length !== 0){
this.fullFilterKey()
}
}
fullFilterKey(){
const self = this
Object.keys(this.items[0]).forEach(key => {
self.filterKeys.push(key)
})
}
doInfinite(infiniteScroll: any) {
this.infiniteScroll = infiniteScroll
if (this.isAjaxActive() && !this.areAjaxDataAllLoaded && this.limitCount >= this.items.length){
this.loadAjax.emit()
}else{
setTimeout(() => {
this.completeScroll()
}, 300)
}
}
afterAjax(items){
if (items.length < this.limit){
this.areAjaxDataAllLoaded = true
}
this.completeScroll()
}
completeScroll(){
if(this.infiniteScroll){
this.limitCount = this.limitCount + this.limit
this.infiniteScroll.complete()
}
}
showInfinitScroll() {
if (this.isAjaxActive() && !this.areAjaxDataAllLoaded){
return true
}else{
return this.searchPipe.transform(this.items, this.filterKeys, this.filter).length >= this.limitCount
}
}
isAjaxActive(){
return this.loadAjax.observers.length > 0
}
}