@@ -4,37 +4,33 @@ import 'igniteui-webcomponents-grids/grids/combined';
44import 'igniteui-webcomponents-grids/grids/themes/light/bootstrap.css' ;
55import { html } from 'lit-html' ;
66import { RemotePagingService } from './RemoteService' ;
7+ import { CustomersWithPageResponseModel } from './CustomersWithPageResponseModel' ;
78
89export class Sample {
910
1011 public data : any [ ] = [ ] ;
11- public page = 0 ;
12- private _perPage = 10 ;
13- public pager : IgcPaginatorComponent ;
12+ public page : number = 0 ;
1413 public hierarchicalGrid : IgcHierarchicalGridComponent ;
15- private remoteService : RemotePagingService = new RemotePagingService ( ) ;
14+ private _bind : ( ) => void ;
15+ private _perPage = 15 ;
16+ public pager : IgcPaginatorComponent ;
17+ private _totalRecordsCount : number ;
1618
1719 public get perPage ( ) : number {
18- return this . pager ?. perPage || 10 ;
20+ return this . pager ?. perPage || this . _perPage ;
1921 }
2022 public set perPage ( val : number ) {
2123 this . _perPage = val ;
22- this . paginate ( val ) ;
2324 }
24-
25- private _totalRecordsCount : number ;
26-
2725 public get totalRecordsCount ( ) : number {
28- return this . _totalRecordsCount ;
26+ return this . _totalRecordsCount ;
2927 }
30-
3128 public set totalRecordsCount ( value : number ) {
32- this . _totalRecordsCount = value ;
33- this . hierarchicalGrid . totalRecords = value ;
29+ this . _totalRecordsCount = value ;
30+ this . hierarchicalGrid . totalRecords = value ;
3431 }
3532
3633 constructor ( ) {
37-
3834 this . hierarchicalGrid = document . getElementById ( "hGrid" ) as IgcHierarchicalGridComponent ;
3935 this . pager = document . getElementById ( 'paginator' ) as IgcPaginatorComponent ;
4036 const ordersRowIsland = document . getElementById ( "ordersRowIsland" ) as IgcRowIslandComponent ;
@@ -43,81 +39,89 @@ export class Sample {
4339 ordersRowIsland . paginatorTemplate = this . webHierarchicalGridPaginatorTemplate ;
4440 orderDetailsRowIsland . paginatorTemplate = this . webHierarchicalGridPaginatorTemplate ;
4541
46- this . pager . addEventListener ( "perPageChange" , ( ) => {
47- this . paginate ( 0 ) ;
48- } ) ;
49- this . pager . addEventListener ( "pageChange" , ( ( args : CustomEvent < any > ) => {
50- this . paginate ( args . detail ) ; } ) as EventListener ) ;
51-
52-
53- ordersRowIsland . addEventListener ( "gridCreated" , ( event : any ) => {
54- this . gridCreated ( event , "Customers" ) ;
55- } ) ;
56-
57- orderDetailsRowIsland . addEventListener ( "gridCreated" , ( event : any ) => {
58- this . gridCreated ( event , "Orders" ) ;
59- } ) ;
60-
61- window . addEventListener ( "load" , ( ) => {
62- this . pager . totalRecords = this . totalRecordsCount ;
63- this . paginate ( 0 ) ;
64- } ) ;
65-
66- this . hierarchicalGrid . data = this . data ;
67-
68- this . hierarchicalGrid . isLoading = true ;
69- this . remoteService . getData ( { parentID : null , rootLevel : true , key : "Customers" } , this . page , this . _perPage ) . then ( ( data : any ) => {
70- this . hierarchicalGrid . isLoading = false ;
71- this . hierarchicalGrid . data = data ;
72- this . hierarchicalGrid . markForCheck ( ) ;
73- } ) ;
74- this . remoteService . getDataLength ( { parentID : null , rootLevel : true , key : "Customers" } ) . then ( ( length : number ) => {
75- if ( length !== undefined ) {
76- this . totalRecordsCount = length ;
77- this . pager . totalRecords = this . totalRecordsCount ;
78- }
79- } ) ;
42+ this . _bind = ( ) => {
43+ window . addEventListener ( "load" , ( ) => {
44+ this . pager . perPage = this . _perPage ;
45+ this . loadCustomersData ( this . page , this . perPage ) ;
46+ } ) ;
47+
48+ this . pager . addEventListener ( "perPageChange" , ( ( args : CustomEvent < any > ) => {
49+ this . perPage = args . detail ;
50+ this . loadCustomersData ( this . page , this . perPage ) ;
51+ } ) as EventListener ) ;
52+
53+ this . pager . addEventListener ( "pageChange" , ( ( args : CustomEvent < any > ) => {
54+ this . page = args . detail ;
55+ this . loadCustomersData ( this . page , this . perPage ) ;
56+ } ) as EventListener ) ;
57+
58+ ordersRowIsland . addEventListener ( "gridCreated" , ( event : any ) => {
59+ this . gridCreated ( event , "Customers" ) ;
60+ } ) ;
61+
62+ orderDetailsRowIsland . addEventListener ( "gridCreated" , ( event : any ) => {
63+ this . gridCreated ( event , "Orders" ) ;
64+ } ) ;
65+ }
66+
67+ this . _bind ( ) ;
8068 }
8169
82- public gridCreated ( event : CustomEvent < IgcGridCreatedEventArgs > , _parentKey : string ) {
70+ public gridCreated ( event : CustomEvent < IgcGridCreatedEventArgs > , parentKey : string ) {
8371 const context = event . detail ;
84-
85- const dataState = {
86- key : context . owner . childDataKey ,
87- parentID : context . parentID ,
88- parentKey : _parentKey ,
89- rootLevel : false
90- } ;
72+ const parentId : string = context . parentID ;
73+ const childDataKey : string = context . owner . childDataKey ;
9174
9275 context . grid . isLoading = true ;
93- this . remoteService . getDataLength ( dataState ) . then ( ( length : number ) => {
94- if ( length !== undefined ) {
95- this . pager . totalRecords = length ;
96- }
97-
98- this . remoteService . getData ( dataState , this . page , this . perPage ) . then ( ( data : any [ ] ) => {
99- context . grid . isLoading = false ;
100- context . grid . data = data ;
101- context . grid . markForCheck ( ) ;
102- } ) ; } ) ;
103- }
104-
105- public paginate ( page : number ) {
106- this . page = page ;
107- const skip = this . page * this . perPage ;
108- const top = this . perPage ;
109-
110- this . remoteService . getData ( { parentID : null , rootLevel : true , key : 'Customers' } , skip , top ) . then ( ( data :any ) => {
111- this . data = data ; // Assign received data to this.data
112- this . hierarchicalGrid . isLoading = false ;
113- this . hierarchicalGrid . markForCheck ( ) ; // Update the UI after receiving data
76+ RemotePagingService . getHierarchyDataById ( parentKey , parentId , childDataKey )
77+ . then ( ( data : any ) => {
78+ context . grid . data = data ;
79+ context . grid . isLoading = false ;
80+ context . grid . markForCheck ( ) ;
81+ } )
82+ . catch ( ( error ) => {
83+ console . error ( error . message ) ;
84+ context . grid . data = [ ] ;
85+ context . grid . isLoading = false ;
86+ context . grid . markForCheck ( ) ;
11487 } ) ;
11588 }
11689
11790 public webHierarchicalGridPaginatorTemplate = ( ) => {
118- return html `< igc-paginator id ="islandPaginator " @perPage ="{this.perPage} " @perPageChanged ="{this.paginate(0)} " per-page ="5 ">
91+ // Child level grids have LOCAL paging
92+ // They can be set to REMOTE if there are endpoints with paging for each hierarchy level data.
93+ return html `
94+ < igc-paginator
95+ id ="islandPaginator ">
11996 </ igc-paginator > `
12097 }
98+
99+ private updateUI ( ) : void {
100+ if ( this . hierarchicalGrid && this . data ) { // Check if grid and data are available
101+ this . hierarchicalGrid . data = this . data ;
102+ }
103+ }
104+
105+ private loadCustomersData ( pageIndex ?: number , pageSize ?: number ) : void {
106+ this . hierarchicalGrid . isLoading = true ;
107+
108+ RemotePagingService . getDataWithPaging ( pageIndex , pageSize )
109+ . then ( ( response : CustomersWithPageResponseModel ) => {
110+ this . totalRecordsCount = response . totalRecordsCount ;
111+ this . pager . perPage = pageSize ;
112+ this . pager . totalRecords = this . totalRecordsCount ;
113+ this . page = response . pageNumber ;
114+ this . data = response . items ;
115+ this . hierarchicalGrid . isLoading = false ;
116+ this . updateUI ( ) ; // Update the UI after receiving data
117+ } )
118+ . catch ( ( error ) => {
119+ console . error ( error . message ) ;
120+ this . hierarchicalGrid . data = [ ] ;
121+ this . hierarchicalGrid . isLoading = false ;
122+ this . updateUI ( ) ;
123+ } )
124+ }
121125}
122126
123127new Sample ( ) ;
0 commit comments