@@ -3,77 +3,83 @@ import { IgcGridComponent, IgcPaginatorComponent } from 'igniteui-webcomponents-
33import "igniteui-webcomponents-grids/grids/themes/light/bootstrap.css" ;
44import "./index.css" ;
55import { RemotePagingService } from './RemotePagingService' ;
6+ import { CustomersWithPageResponseModel } from './CustomersWithPageResponseModel' ;
67
78
89export class Sample {
910
10- public data : any [ ] = [ ] ;
11- public dataLength : number = 0 ;
12- private grid : IgcGridComponent ;
13- private _bind : ( ) => void ;
14- private remotePagingService : RemotePagingService = new RemotePagingService ( ) ;
15- public page = 0 ;
16- private _perPage = 10 ;
17- private pager : IgcPaginatorComponent ;
18-
19- public get perPage ( ) : number {
20- return this . pager . perPage || 10 ;
21- }
22-
23- private _totalRecordsCount : number ;
24-
25- public get totalRecordsCount ( ) : number {
11+ public data : any [ ] = [ ] ;
12+ public dataLength : number = 0 ;
13+ public page : number = 0 ;
14+ private grid : IgcGridComponent ;
15+ private _bind : ( ) => void ;
16+ private _perPage : number = 15 ;
17+ private pager : IgcPaginatorComponent ;
18+ private _totalRecordsCount : number ;
19+ public get perPage ( ) : number {
20+ return this . pager . perPage || this . _perPage ;
21+ }
22+ public set perPage ( val : number ) {
23+ this . _perPage = val ;
24+ }
25+ public get totalRecordsCount ( ) : number {
2626 return this . _totalRecordsCount ;
27- }
28-
29- public set totalRecordsCount ( value : number ) {
27+ }
28+ public set totalRecordsCount ( value : number ) {
3029 this . _totalRecordsCount = value ;
3130 this . grid . totalRecords = value ;
32- }
33-
34- constructor ( ) {
31+ }
32+
33+ constructor ( ) {
3534 this . pager = document . getElementById ( 'paginator' ) as IgcPaginatorComponent ;
3635 this . grid = document . getElementById ( 'grid' ) as IgcGridComponent ;
3736
3837 this . _bind = ( ) => {
39- this . remotePagingService . getDataLength ( ) . then ( ( length ) => {
40- this . totalRecordsCount = length ;
41- this . pager . totalRecords = this . totalRecordsCount ;
42- } ) ;
43- window . addEventListener ( "load" , ( ) => {
44- this . pager . totalRecords = this . totalRecordsCount ;
45- this . paginate ( 0 ) ;
46- } ) ;
47- this . pager . addEventListener ( "perPageChange" , ( ) => {
48- this . paginate ( 0 ) ;
49- } )
50- this . pager . addEventListener ( "pageChange" , ( ( args : CustomEvent < any > ) => {
51- this . paginate ( args . detail ) ; } ) as EventListener ) ;
52- }
53- this . _bind ( ) ;
54- }
5538
56- public paginate ( page : number ) {
57- this . page = page ;
58- const skip = this . page * this . perPage ;
59- const top = this . perPage ;
60-
61- this . remotePagingService . getData ( skip , top ) . then ( ( data ) => {
62- this . data = data ; // Assign received data to this.data
63- this . grid . isLoading = false ;
64- this . updateUI ( ) ; // Update the UI after receiving data
39+ window . addEventListener ( "load" , ( ) => {
40+ this . loadData ( this . page , this . perPage ) ;
6541 } ) ;
66- }
6742
68- public set perPage ( val : number ) {
69- this . _perPage = val ;
70- this . paginate ( val ) ;
71- }
43+ this . pager . addEventListener ( "perPageChange" , ( ( args : CustomEvent < any > ) => {
44+ this . perPage = args . detail ;
45+ this . loadData ( this . page , this . perPage ) ;
46+ } ) as EventListener ) ;
7247
73- private updateUI ( ) {
74- if ( this . grid && this . data ) { // Check if grid and data are available
75- this . grid . data = this . data ;
48+ this . pager . addEventListener ( "pageChange" , ( ( args : CustomEvent < any > ) => {
49+ this . page = args . detail ;
50+ this . loadData ( this . page , this . perPage ) ;
51+ } ) as EventListener ) ;
7652 }
53+
54+ this . _bind ( ) ;
55+ }
56+
57+ private updateUI ( ) : void {
58+ if ( this . grid && this . data ) { // Check if grid and data are available
59+ this . grid . data = this . data ;
60+ }
61+ }
62+
63+ private loadData ( pageIndex ?: number , pageSize ?: number ) : void {
64+ this . grid . isLoading = true ;
65+
66+ RemotePagingService . getDataWithPaging ( pageIndex , pageSize )
67+ . then ( ( response : CustomersWithPageResponseModel ) => {
68+ this . totalRecordsCount = response . totalRecordsCount ;
69+ this . pager . perPage = pageSize ;
70+ this . pager . totalRecords = this . totalRecordsCount ;
71+ this . page = response . pageNumber ;
72+ this . data = response . items ;
73+ this . grid . isLoading = false ;
74+ this . updateUI ( ) ; // Update the UI after receiving data
75+ } )
76+ . catch ( ( error ) => {
77+ console . error ( error . message ) ;
78+ this . grid . data = [ ] ;
79+ // Stop loading even if error occurs. Prevents endless loading
80+ this . grid . isLoading = false ;
81+ this . updateUI ( ) ;
82+ } )
7783 }
7884
7985}
0 commit comments