Skip to content

Commit bed9f73

Browse files
committed
fix(HGrid Remote Paging): refactored sample with the new customers endpoint
1 parent a384668 commit bed9f73

File tree

5 files changed

+123
-113
lines changed

5 files changed

+123
-113
lines changed

samples/grids/grid/remote-paging-grid/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import { CustomersWithPageResponseModel } from './CustomersWithPageResponseModel
99
export class Sample {
1010

1111
public data: any[] = [];
12-
public dataLength: number = 0;
1312
public page: number = 0;
1413
private grid: IgcGridComponent;
1514
private _bind: () => void;
1615
private _perPage: number = 15;
1716
private pager: IgcPaginatorComponent;
1817
private _totalRecordsCount: number;
18+
1919
public get perPage(): number {
2020
return this.pager.perPage || this._perPage;
2121
}
@@ -31,11 +31,10 @@ export class Sample {
3131
}
3232

3333
constructor() {
34-
this.pager = document.getElementById('paginator') as IgcPaginatorComponent;
3534
this.grid = document.getElementById('grid') as IgcGridComponent;
35+
this.pager = document.getElementById('paginator') as IgcPaginatorComponent;
3636

3737
this._bind = () => {
38-
3938
window.addEventListener("load", () => {
4039
this.loadData(this.page,this.perPage);
4140
});
@@ -75,7 +74,6 @@ export class Sample {
7574
})
7675
.catch((error) => {
7776
console.error(error.message);
78-
this.grid.data = [];
7977
// Stop loading even if error occurs. Prevents endless loading
8078
this.grid.isLoading = false;
8179
this.updateUI();

samples/grids/hierarchical-grid/remote-paging-sample/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
<div class="container sample">
2020
<div class="container fill">
2121

22-
<igc-hierarchical-grid id="hGrid" primary-key="customerId" height="600px">
22+
<igc-hierarchical-grid
23+
id="hGrid"
24+
primary-key="customerId"
25+
height="600px"
26+
paging-mode="Remote"
27+
>
2328
<igc-paginator id="paginator">
2429
</igc-paginator>
2530
<igc-column field="customerId" hidden="true"></igc-column>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface CustomersWithPageResponseModel {
2+
items: any[];
3+
totalRecordsCount: number;
4+
pageSize: number;
5+
pageNumber: number;
6+
totalPages: number;
7+
}

samples/grids/hierarchical-grid/remote-paging-sample/src/RemoteService.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,38 @@ function buildUrl(dataState: any, index?: number, perPage?: number): string {
2525
}
2626

2727
export class RemotePagingService {
28-
public remoteData: BehaviorSubject<any[]> = new BehaviorSubject([]);
29-
public dataLength: BehaviorSubject<number> = new BehaviorSubject(0);
30-
public url = 'https://www.igniteui.com/api/products';
28+
public static BASE_URL = 'https://data-northwind.indigo.design/';
29+
public static CUSTOMERS_URL = `${RemotePagingService.BASE_URL}Customers/GetCustomersWithPage`;
3130

3231
constructor() {}
3332

34-
public async getData(dataState?: any, index?: number, perPage?: number): Promise<any> {
35-
try {
36-
const response = await fetch(buildUrl(dataState, index, perPage));
37-
if (!response.ok) {
38-
throw new Error(`HTTP error! status: ${response.status}`);
39-
}
40-
const data = await response.json();
41-
return data;
42-
} catch (error) {
43-
console.error('Error fetching data:', error);
44-
throw error;
45-
}
33+
public static getDataWithPaging(pageIndex?: number, pageSize?: number) {
34+
return fetch(RemotePagingService.buildUrl(RemotePagingService.CUSTOMERS_URL, pageIndex, pageSize))
35+
.then((result) => result.json())
36+
.catch((error) => console.error(error.message));
37+
}
38+
39+
public static getHierarchyDataById(parentEntityName: string, parentId: string, childEntityName: string) {
40+
return fetch(`${RemotePagingService.BASE_URL}${parentEntityName}/${parentId}/${childEntityName}`)
41+
.then((result) => result.json());
4642
}
4743

48-
public async getDataLength(dataState: any): Promise<number> {
49-
try {
50-
const response = await fetch(buildUrl(dataState));
51-
if (!response.ok) {
52-
throw new Error(`HTTP error! status: ${response.status}`);
53-
}
54-
const data = await response.json();
55-
if (Array.isArray(data)) {
56-
return data.length;
57-
} else {
58-
console.error('Data is not an array:', data);
59-
return 0;
44+
private static buildUrl(baseUrl: string, pageIndex?: number, pageSize?: number) {
45+
let qS = "";
46+
if (baseUrl) {
47+
qS += `${baseUrl}`;
48+
}
49+
50+
// Add pageIndex and size to the query string if they are defined
51+
if (pageIndex !== undefined) {
52+
qS += `?pageIndex=${pageIndex}`;
53+
if (pageSize !== undefined) {
54+
qS += `&size=${pageSize}`;
6055
}
61-
} catch (error) {
62-
console.error('Error fetching data length:', error);
63-
return 0;
56+
} else if (pageSize !== undefined) {
57+
qS += `?perPage=${pageSize}`;
6458
}
59+
60+
return `${qS}`;
6561
}
6662
}

samples/grids/hierarchical-grid/remote-paging-sample/src/index.ts

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,33 @@ import 'igniteui-webcomponents-grids/grids/combined';
44
import 'igniteui-webcomponents-grids/grids/themes/light/bootstrap.css';
55
import { html } from 'lit-html';
66
import { RemotePagingService } from './RemoteService';
7+
import { CustomersWithPageResponseModel } from './CustomersWithPageResponseModel';
78

89
export 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

123127
new Sample();

0 commit comments

Comments
 (0)