Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions samples/grids/hierarchical-grid/infinite-scroll/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hierarchical Grid Infinite Scroll</title>
</head>
<body>
<div id="root">
<igc-hierarchical-grid id="grid" aria-label="Customers and orders" primary-key="customerId"
height="480px" auto-generate="false">
<igc-column field="companyName" header="Company"></igc-column>
<igc-column field="contactName" header="Contact"></igc-column>
<igc-column field="address.country" header="Country"></igc-column>
<igc-row-island id="orders" child-data-key="Orders" primary-key="orderId" auto-generate="false">
<igc-column field="orderId" header="Order ID" data-type="number"></igc-column>
<igc-column field="shipAddress.city" header="Ship City"></igc-column>
</igc-row-island>
</igc-hierarchical-grid>
</div>
<script type="module" src="src/index.ts"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions samples/grids/hierarchical-grid/infinite-scroll/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "hierarchical-grid-infinite-scroll",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build"
},
"dependencies": {
"igniteui-webcomponents-grids": "^7.4.0"
},
"devDependencies": {
"typescript": "^6.0.3",
"vite": "^8.2.0"
}
}
89 changes: 89 additions & 0 deletions samples/grids/hierarchical-grid/infinite-scroll/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'igniteui-webcomponents-grids/grids/combined';
import type { IgcHierarchicalGridComponent, IgcRowIslandComponent } from 'igniteui-webcomponents-grids/grids';
import 'igniteui-webcomponents-grids/grids/themes/light/bootstrap.css';

const northwindUrl = 'https://data-northwind.indigo.design';

interface Customer {
customerId: string;
companyName: string;
contactName: string;
address: { country: string };
}

interface CustomerPage {
items: Customer[];
totalRecordsCount: number;
}

interface Order {
orderId: number;
shipAddress: { city: string };
}

async function fetchData<T>(path: string): Promise<T> {
const response = await fetch(`${northwindUrl}/${path}`);
if (!response.ok) {
throw new Error(`Northwind request failed (${response.status}): ${response.url}`);
}
return response.json();
}

export class Sample {
private readonly grid = document.getElementById('grid') as IgcHierarchicalGridComponent;
private readonly pageSize = 20;
private readonly cache: Customer[] = [];
private nextPage = 0;
private totalRecords = Infinity;
private loading = false;

constructor() {
const orders = document.getElementById('orders') as IgcRowIslandComponent;
orders.addEventListener('gridCreated', async ({ detail: { grid, parentID } }) => {
grid.isLoading = true;
try {
grid.data = await fetchData<Order[]>(`Customers/${encodeURIComponent(parentID)}/Orders`);
} catch (error) {
console.error('Unable to load customer orders.', error);
} finally {
grid.isLoading = false;
grid.markForCheck();
}
});

this.grid.addEventListener('dataPreLoad', ({ detail }) => {
const end = (detail.startIndex ?? 0) + (detail.chunkSize ?? 0);
if (end >= this.cache.length) {
void this.loadNextPage();
}
});

void this.loadNextPage();
}

private async loadNextPage(): Promise<void> {
if (this.loading || this.cache.length >= this.totalRecords) return;
this.loading = true;
this.grid.isLoading = true;
try {
const page = await fetchData<CustomerPage>(
`Customers/GetCustomersWithPage?pageIndex=${this.nextPage}&size=${this.pageSize}`,
);
if (!page.items.length && this.cache.length < page.totalRecordsCount) {
throw new Error(`Northwind returned an empty customer page at index ${this.nextPage}.`);
}
this.totalRecords = page.totalRecordsCount;
this.nextPage++;
this.cache.push(...page.items);
// Let the grid virtualize the loaded rows, including expanded child rows.
this.grid.data = [...this.cache];
} catch (error) {
console.error('Unable to load customer rows.', error);
} finally {
this.loading = false;
this.grid.isLoading = false;
}
}
}

new Sample();
12 changes: 12 additions & 0 deletions samples/grids/hierarchical-grid/infinite-scroll/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2023",
"module": "ESNext",
"moduleResolution": "bundler",
"types": ["vite/client"],
"strict": true,
"skipLibCheck": true,
"noEmit": true
},
"include": ["src"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This folder contains implementation of Web Components application with example o
<a target="_blank" href="./src/index.ts" rel="noopener noreferrer">
<img height="40px" style="border-radius: 0rem; max-width: 100%;" alt="View Code" src="https://dl.infragistics.com/x/img/browsers/button-code.png"/>
</a>
<a target="_blank" href="https://www.infragistics.com/webcomponents-demos/samples/grids/hierarchical-grid/remote-paging-sample" rel="noopener noreferrer">
<a target="_blank" href="https://www.infragistics.com/webcomponents-demos/grids/hierarchical-grid/remote-paging-sample" rel="noopener noreferrer">
<img height="40px" style="border-radius: 0rem; max-width: 100%;" alt="Run Sample" src="https://dl.infragistics.com/x/img/browsers/button-run.png"/>
</a>
<a target="_blank" href="https://codesandbox.io/s/github/IgniteUI/igniteui-wc-examples/tree/master/samples/grids/hierarchical-grid/remote-paging-sample?fontsize=14&hidenavigation=1&theme=dark&view=preview&file=/src/{SampleFile}" rel="noopener noreferrer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ export class RemotePagingService {

public static getDataWithPaging(pageIndex?: number, pageSize?: number) {
return fetch(RemotePagingService.buildUrl(RemotePagingService.CUSTOMERS_URL, pageIndex, pageSize))
.then((result) => result.json())
.catch((error) => console.error(error.message));
.then(RemotePagingService.readResponse);
}

public static getHierarchyDataById(parentEntityName: string, parentId: string, childEntityName: string) {
return fetch(`${RemotePagingService.BASE_URL}${parentEntityName}/${parentId}/${childEntityName}`)
.then((result) => result.json());
.then(RemotePagingService.readResponse);
}

private static readResponse(response: Response) {
if (!response.ok) {
throw new Error(`Northwind request failed (${response.status}): ${response.url}`);
}
return response.json();
}

private static buildUrl(baseUrl: string, pageIndex?: number, pageSize?: number) {
Expand Down
31 changes: 15 additions & 16 deletions samples/grids/hierarchical-grid/remote-paging-sample/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class Sample {
private _bind: () => void;
private _perPage = 15;
public pager: IgcPaginatorComponent;
private _totalRecordsCount: number;
private _totalRecordsCount = 0;
private requestId = 0;

public get perPage(): number {
return this.pager?.perPage || this._perPage;
Expand All @@ -41,20 +42,16 @@ export class Sample {
orderDetailsRowIsland.paginatorTemplate = this.webHierarchicalGridPaginatorTemplate;

this._bind = () => {
window.addEventListener("load", () => {
this.pager.perPage = this._perPage;
this.loadCustomersData(this.page,this.perPage);
});

this.pager.addEventListener("perPageChange", ((args: CustomEvent<any>) => {
this.pager.addEventListener("perPageChange", (args: CustomEvent<number>) => {
this.perPage = args.detail;
this.loadCustomersData(this.page, this.perPage);
}) as EventListener);
this.page = this.pager.page;
this.loadCustomersData(this.page, args.detail);
});

this.pager.addEventListener("pageChange", ((args: CustomEvent<any>) => {
this.pager.addEventListener("pageChange", (args: CustomEvent<number>) => {
this.page = args.detail;
this.loadCustomersData(this.page, this.perPage);
}) as EventListener);
});

ordersRowIsland.addEventListener("gridCreated", (event: any) => {
this.gridCreated(event, "Customers");
Expand All @@ -66,6 +63,7 @@ export class Sample {
}

this._bind();
this.loadCustomersData(this.page, this._perPage);
}

public gridCreated(event: CustomEvent<IgcGridCreatedEventArgs>, parentKey: string) {
Expand Down Expand Up @@ -103,24 +101,25 @@ export class Sample {
}
}

private loadCustomersData(pageIndex?: number, pageSize?: number): void {
private loadCustomersData(pageIndex: number, pageSize: number): void {
const requestId = ++this.requestId;
this.hierarchicalGrid.isLoading = true;

RemotePagingService.getDataWithPaging(pageIndex,pageSize)
.then((response: CustomersWithPageResponseModel) => {
if (requestId !== this.requestId) return;
this.totalRecordsCount = response.totalRecordsCount;
this.pager.perPage = pageSize;
this.pager.totalRecords = this.totalRecordsCount;
this.page = response.pageNumber;
this.data = response.items;
this.hierarchicalGrid.isLoading = false;
this.updateUI(); // Update the UI after receiving data
})
.catch((error) => {
console.error(error.message);
this.hierarchicalGrid.data = [];
this.hierarchicalGrid.isLoading = false;
this.updateUI();
})
.finally(() => {
if (requestId === this.requestId) this.hierarchicalGrid.isLoading = false;
})
}
}
Expand Down