|
| 1 | +import { AfterViewInit, Component, OnDestroy, OnInit, TemplateRef, ViewChild, ViewEncapsulation } from "@angular/core"; |
| 2 | +import { IgxGridComponent } from "igniteui-angular"; |
| 3 | +import { Observable } from "rxjs"; |
| 4 | +import { RemotePagingService } from "../services/remotePagingService"; |
| 5 | +@Component({ |
| 6 | + encapsulation: ViewEncapsulation.None, |
| 7 | + providers: [RemotePagingService], |
| 8 | + selector: "custom-remote-paging-grid-sample", |
| 9 | + styleUrls: ["./custom-remote-paging-sample.component.scss"], |
| 10 | + templateUrl: "./custom-remote-paging-sample.component.html" |
| 11 | +}) |
| 12 | +export class CustomRemotePagingGridSample implements OnInit, AfterViewInit, OnDestroy { |
| 13 | + |
| 14 | + public page = 0; |
| 15 | + public lastPage = false; |
| 16 | + public firstPage = true; |
| 17 | + public totalPages: number = 1; |
| 18 | + public totalCount = 0; |
| 19 | + public pages = []; |
| 20 | + public title = "gridPaging"; |
| 21 | + public data: Observable<any[]>; |
| 22 | + |
| 23 | + @ViewChild("customPager", { read: TemplateRef, static: true }) public remotePager: TemplateRef<any>; |
| 24 | + @ViewChild("grid1", { static: true }) public grid1: IgxGridComponent; |
| 25 | + |
| 26 | + private visibleElements = 5; |
| 27 | + private _perPage = 10; |
| 28 | + private _dataLengthSubscriber; |
| 29 | + |
| 30 | + constructor( |
| 31 | + private remoteService: RemotePagingService) { |
| 32 | + } |
| 33 | + |
| 34 | + public get perPage(): number { |
| 35 | + return this._perPage; |
| 36 | + } |
| 37 | + |
| 38 | + public set perPage(val: number) { |
| 39 | + this._perPage = val; |
| 40 | + this.paginate(0, true); |
| 41 | + } |
| 42 | + |
| 43 | + public get shouldShowLastPage() { |
| 44 | + return this.pages[this.pages.length - 1] !== this.totalPages - 1; |
| 45 | + } |
| 46 | + |
| 47 | + public get shouldShowFirstPage() { |
| 48 | + return this.pages[0] !== 0; |
| 49 | + } |
| 50 | + |
| 51 | + public ngOnInit() { |
| 52 | + this.data = this.remoteService.remoteData.asObservable(); |
| 53 | + |
| 54 | + this._dataLengthSubscriber = this.remoteService.getDataLength().subscribe((data) => { |
| 55 | + this.totalCount = data; |
| 56 | + this.totalPages = Math.ceil(data / this.perPage); |
| 57 | + this.buttonDeselection(this.page, this.totalPages); |
| 58 | + this.grid1.isLoading = false; |
| 59 | + this.setNumberOfPagingItems(this.page, this.totalPages); |
| 60 | + }); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + public ngOnDestroy() { |
| 65 | + if (this._dataLengthSubscriber) { |
| 66 | + this._dataLengthSubscriber.unsubscribe(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public ngAfterViewInit() { |
| 71 | + this.grid1.isLoading = true; |
| 72 | + this.remoteService.getData(0, this.perPage); |
| 73 | + } |
| 74 | + |
| 75 | + public nextPage() { |
| 76 | + this.firstPage = false; |
| 77 | + this.page++; |
| 78 | + const skip = this.page * this.perPage; |
| 79 | + const top = this.perPage; |
| 80 | + this.remoteService.getData(skip, top); |
| 81 | + if (this.page + 1 >= this.totalPages) { |
| 82 | + this.lastPage = true; |
| 83 | + } |
| 84 | + this.setNumberOfPagingItems(this.page, this.totalPages); |
| 85 | + } |
| 86 | + |
| 87 | + public previousPage() { |
| 88 | + this.lastPage = false; |
| 89 | + this.page--; |
| 90 | + const skip = this.page * this.perPage; |
| 91 | + const top = this.perPage; |
| 92 | + this.remoteService.getData(skip, top); |
| 93 | + if (this.page <= 0) { |
| 94 | + this.firstPage = true; |
| 95 | + } |
| 96 | + this.setNumberOfPagingItems(this.page, this.totalPages); |
| 97 | + } |
| 98 | + |
| 99 | + public paginate(page: number, recalc = false) { |
| 100 | + this.page = page; |
| 101 | + const skip = this.page * this.perPage; |
| 102 | + const top = this.perPage; |
| 103 | + if (recalc) { |
| 104 | + this.totalPages = Math.ceil(this.totalCount / this.perPage); |
| 105 | + } |
| 106 | + this.setNumberOfPagingItems(this.page, this.totalPages); |
| 107 | + this.remoteService.getData(skip, top); |
| 108 | + this.buttonDeselection(this.page, this.totalPages); |
| 109 | + } |
| 110 | + |
| 111 | + public buttonDeselection(page: number, totalPages: number) { |
| 112 | + if (totalPages === 1) { |
| 113 | + this.lastPage = true; |
| 114 | + this.firstPage = true; |
| 115 | + } else if (page + 1 >= totalPages) { |
| 116 | + this.lastPage = true; |
| 117 | + this.firstPage = false; |
| 118 | + } else if (page !== 0 && page !== totalPages) { |
| 119 | + this.lastPage = false; |
| 120 | + this.firstPage = false; |
| 121 | + } else { |
| 122 | + this.lastPage = false; |
| 123 | + this.firstPage = true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public activePage(page) { |
| 128 | + return page === this.page ? "activePage" : ""; |
| 129 | + } |
| 130 | + |
| 131 | + public setNumberOfPagingItems(currentPage, totalPages) { |
| 132 | + if (currentPage > this.pages[0] && currentPage < this.pages[this.pages.length]) { |
| 133 | + return; |
| 134 | + } |
| 135 | + if (this.pages.length === 0) { |
| 136 | + const lastPage = (currentPage + this.visibleElements) <= totalPages ? |
| 137 | + currentPage + this.visibleElements : totalPages; |
| 138 | + for (let item = 0; item < lastPage; item++) { |
| 139 | + this.pages.push(item); |
| 140 | + } |
| 141 | + return; |
| 142 | + } |
| 143 | + if (currentPage <= this.pages[0]) { |
| 144 | + this.pages = []; |
| 145 | + let firstPage = currentPage - 1 < 0 ? 0 : currentPage - 1; |
| 146 | + firstPage = firstPage > totalPages - this.visibleElements ? |
| 147 | + totalPages - this.visibleElements : firstPage; |
| 148 | + firstPage = firstPage >= 0 ? firstPage : 0; |
| 149 | + const lastPage = (firstPage + this.visibleElements) <= totalPages ? |
| 150 | + firstPage + this.visibleElements : totalPages; |
| 151 | + for (let item = firstPage; item < lastPage; item++) { |
| 152 | + this.pages.push(item); |
| 153 | + } |
| 154 | + |
| 155 | + } else if (currentPage >= this.pages[this.pages.length - 1]) { |
| 156 | + this.pages = []; |
| 157 | + let firstPage = currentPage > totalPages - this.visibleElements ? |
| 158 | + totalPages - this.visibleElements : currentPage - 1; |
| 159 | + firstPage = firstPage >= 0 ? firstPage : 0; |
| 160 | + const lastPage = (firstPage + this.visibleElements) <= totalPages ? |
| 161 | + firstPage + this.visibleElements : totalPages; |
| 162 | + for (let item = firstPage; item < lastPage; item++) { |
| 163 | + this.pages.push(item); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments