Skip to content

Commit 3e288f7

Browse files
Remote Paging Grid & Hgrid: Refactoring with the new customers endpoint (#874)
1 parent 5d249f3 commit 3e288f7

File tree

8 files changed

+233
-265
lines changed

8 files changed

+233
-265
lines changed

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

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,43 @@
2626
moving="true"
2727
paging-mode="Remote">
2828
<igc-paginator
29-
id="paginator">
29+
id="paginator">
3030
</igc-paginator>
3131
<igc-column
32-
name="CategoryName"
33-
id="CategoryName"
34-
field="CategoryName"
35-
header="Category Name">
32+
name="customerId"
33+
id="customerId"
34+
field="customerId"
35+
hidden="true">
3636
</igc-column>
3737
<igc-column
38-
name="ImageID"
39-
id="ImageID"
40-
field="CategoryImageUrl"
41-
data-type="image"
42-
header="Category Image">
38+
name="companyName"
39+
id="companyName"
40+
field="companyName"
41+
header="Company Name">
4342
</igc-column>
4443
<igc-column
45-
name="ProductName"
46-
id="ProductName"
47-
field="ProductName"
48-
header="Product Name">
44+
name="contactName"
45+
id="contactName"
46+
field="contactName"
47+
header="Contact Name">
4948
</igc-column>
5049
<igc-column
51-
name="QuantityPerUnit"
52-
id="QuantityPerUnit"
53-
field="QuantityPerUnit"
54-
header="Quantity Per Unit">
50+
name="contactTitle"
51+
id="contactTitle"
52+
field="contactTitle"
53+
header="Contact Title">
5554
</igc-column>
5655
<igc-column
57-
name="UnitPrice"
58-
id="UnitPrice"
59-
field="UnitPrice"
60-
header="Unit Price">
56+
name="address?.country"
57+
id="address.country"
58+
field="address.country"
59+
header="Country">
6160
</igc-column>
6261
<igc-column
63-
name="SupplierName"
64-
id="SupplierName"
65-
field="SupplierName"
66-
header="Supplier Name">
67-
</igc-column>
68-
<igc-column
69-
name="UnitsInStock"
70-
id="UnitsInStock"
71-
field="UnitsInStock"
72-
header="Units In Stock">
73-
</igc-column>
74-
<igc-column
75-
name="UnitsOnOrder"
76-
id="UnitsOnOrder"
77-
field="UnitsOnOrder"
78-
header="Units On Order">
62+
name="address.phone"
63+
id="address.phone"
64+
field="address.phone"
65+
header="Phone">
7966
</igc-column>
8067
</igc-grid>
8168
</div>
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+
}
Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,31 @@
1-
import { BehaviorSubject, Observable, from } from 'rxjs';
2-
import { map, catchError } from 'rxjs/operators';
3-
4-
51
export class RemotePagingService {
6-
public remoteData: BehaviorSubject<any[]> = new BehaviorSubject([]);
7-
public dataLength: BehaviorSubject<number> = new BehaviorSubject(0);
8-
public url = 'https://www.igniteui.com/api/products';
9-
10-
constructor() {}
2+
public static CUSTOMERS_URL = `https://data-northwind.indigo.design/Customers/GetCustomersWithPage`;
3+
constructor() {}
114

12-
public async getData(index?: number, perPage?: number): Promise<any> {
13-
let qS = '';
14-
15-
if (index !== undefined && perPage !== undefined) {
16-
qS = `?$skip=${index}&$top=${perPage}&$count=true`;
5+
public static getDataWithPaging(pageIndex?: number, pageSize?: number) {
6+
return fetch(RemotePagingService.buildUrl(RemotePagingService.CUSTOMERS_URL, pageIndex, pageSize))
7+
.then((result) => result.json())
8+
.catch((error) => console.error(error.message));
179
}
18-
19-
try {
20-
const response = await fetch(`${this.url + qS}`);
21-
if (!response.ok) {
22-
throw new Error('Network response was not ok');
10+
11+
private static buildUrl(baseUrl: string, pageIndex?: number, pageSize?: number) {
12+
let qS = "";
13+
if (baseUrl) {
14+
qS += `${baseUrl}`;
2315
}
24-
const data = await response.json();
25-
return data;
26-
} catch (error) {
27-
console.error('Error fetching data:', error);
28-
throw error; // Propagate the error further
29-
}
30-
}
3116

32-
public async getDataLength(): Promise<number> {
33-
try {
34-
const response = await fetch(this.url);
35-
if (!response.ok) {
36-
throw new Error('Network response was not ok');
17+
// Add pageIndex and size to the query string if they are defined
18+
if (pageIndex !== undefined) {
19+
qS += `?pageIndex=${pageIndex}`;
20+
if (pageSize !== undefined) {
21+
qS += `&size=${pageSize}`;
22+
}
23+
} else if (pageSize !== undefined) {
24+
qS += `?perPage=${pageSize}`;
3725
}
38-
const data = await response.json();
39-
return data.length; // Assuming the length is directly accessible in the JSON response
40-
} catch (error) {
41-
console.error('Error fetching data length:', error);
42-
throw error; // Propagate the error further
43-
}
44-
}
45-
4626

27+
return `${qS}`;
28+
}
4729
}
4830

4931

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

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,80 @@ import { IgcGridComponent, IgcPaginatorComponent } from 'igniteui-webcomponents-
33
import "igniteui-webcomponents-grids/grids/themes/light/bootstrap.css";
44
import "./index.css";
55
import { RemotePagingService } from './RemotePagingService';
6+
import { CustomersWithPageResponseModel } from './CustomersWithPageResponseModel';
67

78
export class Sample {
89

9-
public data: any[] = [];
10-
public dataLength: number = 0;
11-
private grid: IgcGridComponent;
12-
private _bind: () => void;
13-
private remotePagingService: RemotePagingService = new RemotePagingService();
14-
public page = 0;
15-
private _perPage = 10;
16-
private pager: IgcPaginatorComponent;
10+
public data: any[] = [];
11+
public page: number = 0;
12+
private grid: IgcGridComponent;
13+
private _bind: () => void;
14+
private _perPage: number = 15;
15+
private pager: IgcPaginatorComponent;
16+
private _totalRecordsCount: number;
1717

18-
public get perPage(): number {
19-
return this.pager.perPage || 10;
20-
}
21-
22-
private _totalRecordsCount: number;
23-
24-
public get totalRecordsCount(): number {
18+
public get perPage(): number {
19+
return this.pager.perPage || this._perPage;
20+
}
21+
public set perPage(val: number) {
22+
this._perPage = val;
23+
}
24+
public get totalRecordsCount(): number {
2525
return this._totalRecordsCount;
26-
}
27-
28-
public set totalRecordsCount(value: number) {
26+
}
27+
public set totalRecordsCount(value: number) {
2928
this._totalRecordsCount = value;
3029
this.grid.totalRecords = value;
31-
}
30+
}
3231

33-
constructor() {
34-
this.pager = document.getElementById('paginator') as IgcPaginatorComponent;
32+
constructor() {
3533
this.grid = document.getElementById('grid') as IgcGridComponent;
36-
34+
this.pager = document.getElementById('paginator') as IgcPaginatorComponent;
35+
3736
this._bind = () => {
38-
this.remotePagingService.getDataLength().then((length) => {
39-
this.totalRecordsCount = length;
40-
this.pager.totalRecords = this.totalRecordsCount;
41-
});
4237
window.addEventListener("load", () => {
43-
this.pager.totalRecords = this.totalRecordsCount;
44-
this.paginate(0);
45-
});
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-
this._bind();
53-
}
38+
this.loadData(this.page,this.perPage);
39+
});
5440

55-
public paginate(page: number) {
56-
this.page = page;
57-
const skip = this.page * this.perPage;
58-
const top = this.perPage;
41+
this.pager.addEventListener("perPageChange", ((args: CustomEvent<any>) => {
42+
this.perPage = args.detail;
43+
this.loadData(this.page, this.perPage);
44+
}) as EventListener);
5945

60-
this.remotePagingService.getData(skip, top).then((data)=> {
61-
this.data = data; // Assign received data to this.data
62-
this.grid.isLoading = false;
63-
this.updateUI(); // Update the UI after receiving data
64-
});
65-
}
46+
this.pager.addEventListener("pageChange", ((args: CustomEvent<any>) => {
47+
this.page = args.detail;
48+
this.loadData(this.page, this.perPage);
49+
}) as EventListener);
50+
}
51+
52+
this._bind();
53+
}
6654

67-
public set perPage(val: number) {
68-
this._perPage = val;
69-
this.paginate(val);
55+
private updateUI(): void {
56+
if (this.grid && this.data) { // Check if grid and data are available
57+
this.grid.data = this.data;
7058
}
59+
}
7160

72-
private updateUI() {
73-
if (this.grid && this.data) { // Check if grid and data are available
74-
this.grid.data = this.data;
75-
}
61+
private loadData(pageIndex?: number, pageSize?: number): void {
62+
this.grid.isLoading = true;
63+
64+
RemotePagingService.getDataWithPaging(pageIndex,pageSize)
65+
.then((response: CustomersWithPageResponseModel) => {
66+
this.totalRecordsCount = response.totalRecordsCount;
67+
this.pager.perPage = pageSize;
68+
this.pager.totalRecords = this.totalRecordsCount;
69+
this.page = response.pageNumber;
70+
this.data = response.items;
71+
this.grid.isLoading = false;
72+
this.updateUI(); // Update the UI after receiving data
73+
})
74+
.catch((error) => {
75+
console.error(error.message);
76+
// Stop loading even if error occurs. Prevents endless loading
77+
this.grid.isLoading = false;
78+
this.updateUI();
79+
})
7680
}
7781

7882
}

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+
}

0 commit comments

Comments
 (0)