pager.component.ts 4.61 KB
import { Component, OnInit, OnChanges, Input, Output, EventEmitter, 
    Renderer, ElementRef, ViewChild } from "@angular/core";

@Component({
    selector: 'admin-pager',
    templateUrl: './pager.component.html',
})

export class PagerComponent implements OnInit, OnChanges {

    @Output() pagerEvent: EventEmitter<any> = new EventEmitter<any>();
    @Input() pageNo: number;
    @Input() pageLength: number;
    @Input() recordCount: number;
    pageList: any;
    pageShowList: any;
    totalPages: number;
    
    ngOnInit(){
        //this.pageLength = 10;
        this.pageShowList = [];
        for(var i = 0; i < 12; i++){
            this.pageShowList[i] = this.pageNo + i;
        }
        this.pagerEvent.emit(this.pageNo + ', ' + this.pageLength);
        //this.SearchLicenses();
    }

    ngOnChanges(){
        this.pageList = [];
        if ((this.recordCount % this.pageLength) == 0) {
          this.totalPages = parseInt((this.recordCount / this.pageLength).toString());
        }
        else {
          this.totalPages = parseInt((this.recordCount / this.pageLength).toString()) + 1;
        }
        for(var i = 0; i < this.totalPages; i++){
            this.pageList.push(i + 1);
        }
        this.pageShowList = [];
        if(this.totalPages > 12){
            if((this.pageNo % 12) != 0) {
                for(var i = 0; i < 12; i++){
                    this.pageShowList[i] = (this.pageNo - (this.pageNo % 12)) + i + 1;
                }
            }
            else{
                for(var i = 0; i < 12; i++){
                    this.pageShowList[i] = this.pageNo - 12 + i + 1;
                }
            }
        }
        else{
            for(var i = 0; i < 12; i++){
                this.pageShowList[i] = i + 1;
            }
        }
    }

    PageLengthChange(element: any){
        this.pageLength = parseInt(element.value);
        this.pageNo = 1;
        this.pageShowList = [];
        for(var i = 0; i < 12; i++){
            this.pageShowList[i] = this.pageNo + i;
        }
        this.pagerEvent.emit(this.pageNo + ', ' + this.pageLength);
    }

    PageNumberChange(element: any){
        if(element.innerText !=undefined && element.innerText != ''){
            if(element.innerText.indexOf('<') > -1) {
                if(this.pageNo == 1) return;
                this.pageNo = this.pageNo - 1;
                if(this.pageNo < this.pageShowList[0]){
                    this.pageNo = this.pageShowList[0] - 1;
                    for(var i = 0; i < 12; i++){
                        this.pageShowList[i] = this.pageNo - i;
                    }
                }
            } 
            else
            if(element.innerText.indexOf('>') > -1) {
                if(this.pageNo >= this.totalPages) return;
                this.pageNo = this.pageNo + 1;
                if(this.pageNo > this.pageShowList[11]){
                    this.pageNo = this.pageShowList[11] + 1;
                    for(var i = 0; i < 12; i++){
                        this.pageShowList[i] = this.pageNo + i;
                    }
                }
            } 
            else
            if(element.innerText.indexOf('Prev') > -1) {
                if(this.pageShowList[0] == 1) return;
                this.pageNo = this.pageShowList[0] - 12;
                for(var i = 0; i < 12; i++){
                    this.pageShowList[i] = this.pageNo + i;
                }
            } 
            else
            if(element.innerText.indexOf('Next') > -1) {
                if(this.pageShowList[11] >= this.totalPages) return;
                this.pageNo = this.pageShowList[11] + 1;
                for(var i = 0; i < 12; i++){
                    this.pageShowList[i] = this.pageNo + i;
                }
            } 
            else
            {
                if(parseInt(element.innerText) > this.totalPages) return;
                this.pageNo = parseInt(element.innerText);
            }
        }
        else{
            this.pageNo = parseInt(element);
            if(this.totalPages > 12){
                if((this.pageNo % 12) != 0) {
                    for(var i = 0; i < 12; i++){
                        this.pageShowList[i] = (this.pageNo - (this.pageNo % 12)) + i + 1;
                    }
                }
                else{
                    for(var i = 0; i < 12; i++){
                        this.pageShowList[i] = this.pageNo - 12 + i + 1;
                    }
                }
            }
            else{
                for(var i = 0; i < 12; i++){
                    this.pageShowList[i] = i + 1;
                }
            }
        }
        this.pagerEvent.emit(this.pageNo + ', ' + this.pageLength);
    }


}