React Hierarchical Grid のリモート データ操作
デフォルトで、IgrHierarchicalGrid
は独自のロジックを使用してデータ操作を実行します。
これらのタスクをリモートで実行し、IgrHierarchicalGrid
で公開される特定の入力とイベントを使用して IgrHierarchicalGrid
に結果のデータを供給できます。
リモート ページング
const BASE_URL = `https:
const CUSTOMERS_URL = `${BASE_URL}Customers/GetCustomersWithPage`;
export class RemoteService {
public static getCustomersDataWithPaging(pageIndex?: number, pageSize?: number) {
return fetch(this.buildUrl(CUSTOMERS_URL, pageIndex, pageSize))
.then((result) => result.json());
}
public static getHierarchyDataById(parentEntityName: string, parentId: string, childEntityName: string) {
return fetch(`${BASE_URL}${parentEntityName}/${parentId}/${childEntityName}`)
.then((result) => result.json());
}
private static buildUrl(baseUrl: string, pageIndex?: number, pageSize?: number) {
let qS = "";
if (baseUrl) {
qS += `${baseUrl}`;
}
if (pageIndex !== undefined) {
qS += `?pageIndex=${pageIndex}`;
if (pageSize !== undefined) {
qS += `&size=${pageSize}`;
}
} else if (pageSize !== undefined) {
qS += `?perPage=${pageSize}`;
}
return `${qS}`;
}
}
tsx
サービスを宣言した後にコンポーネントを作成する必要があり、IgrHierarchicalGrid
コンストラクションとデータ サブスクリプションを処理します。
次に状態を設定します。
const hierarchicalGrid = useRef<IgrHierarchicalGrid>(null);
const paginator = useRef<IgrPaginator>(null);
const [data, setData] = useState([]);
const [page, setPage] = useState(0);
const [perPage, setPerPage] = useState(15);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
loadGridData(page, perPage);
}, [page, perPage]);
tsx
次に、データを読み込むためのメソッドを設定します。
function loadGridData(pageIndex?: number, pageSize?: number) {
setIsLoading(true);
RemoteService.getCustomersDataWithPaging(pageIndex, pageSize)
.then((response: CustomersWithPageResponseModel) => {
setData(response.items);
setIsLoading(false);
paginator.current.totalRecords = response.totalRecordsCount;
})
.catch((error) => {
console.error(error.message);
setData([]);
setIsLoading(false);
})
}
tsx
最後に、RowIslands の動作を設定します。
function gridCreated(rowIsland: IgrRowIsland, event: IgrGridCreatedEventArgs, parentKey: string) {
const context = event.detail;
const parentId: string = context.parentID;
const childDataKey: string = rowIsland.childDataKey;
RemoteService.getHierarchyDataById(parentKey, parentId, childDataKey)
.then((data: any) => {
context.grid.data = data;
context.grid.isLoading = false;
context.grid.markForCheck();
})
.catch((error) => {
console.error(error.message);
context.grid.data = [];
context.grid.isLoading = false;
context.grid.markForCheck();
})
}
tsx
詳細については、以下の完全なサンプルをご覧ください。
グリッド リモート ページングのデモ
EXAMPLE
CustomersWithPageResponseModel.ts
RemoteService.ts
TSX
CSS
const BASE_URL = `https://data-northwind.indigo.design/`;
const CUSTOMERS_URL = `${BASE_URL}Customers/GetCustomersWithPage`;
export class RemoteService {
public static getCustomersDataWithPaging(pageIndex?: number, pageSize?: number) {
return fetch(this.buildUrl(CUSTOMERS_URL, pageIndex, pageSize))
.then((result) => result.json());
}
public static getHierarchyDataById(parentEntityName: string, parentId: string, childEntityName: string) {
return fetch(`${BASE_URL}${parentEntityName}/${parentId}/${childEntityName}`)
.then((result) => result.json());
}
private static buildUrl(baseUrl: string, pageIndex?: number, pageSize?: number) {
let qS = "";
if (baseUrl) {
qS += `${baseUrl}`;
}
if (pageIndex !== undefined) {
qS += `?pageIndex=${pageIndex}`;
if (pageSize !== undefined) {
qS += `&size=${pageSize}`;
}
} else if (pageSize !== undefined) {
qS += `?perPage=${pageSize}`;
}
return `${qS}`;
}
}
ts
import React, { useEffect, useRef, useState } from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { GridPagingMode, IgrGridCreatedEventArgs, IgrHierarchicalGridModule, IgrPaginator } from "@infragistics/igniteui-react-grids";
import { IgrHierarchicalGrid, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids";
import "@infragistics/igniteui-react-grids/grids/combined";
import "@infragistics/igniteui-react-grids/grids/themes/light/bootstrap.css";
import { RemoteService } from "./RemoteService";
import { IgrNumberEventArgs } from "@infragistics/igniteui-react";
import { CustomersWithPageResponseModel } from "./CustomersWithPageResponseModel";
IgrHierarchicalGridModule.register();
export default function App() {
const hierarchicalGrid = useRef<IgrHierarchicalGrid>(null);
const paginator = useRef<IgrPaginator>(null);
const [data, setData] = useState([]);
const [page, setPage] = useState(0);
const [perPage, setPerPage] = useState(15);
useEffect(() => {
loadGridData(page, perPage);
}, [page, perPage]);
function loadGridData(pageIndex?: number, pageSize?: number) {
hierarchicalGrid.current.isLoading = true;
RemoteService.getCustomersDataWithPaging(pageIndex, pageSize)
.then((response: CustomersWithPageResponseModel) => {
setData(response.items);
hierarchicalGrid.current.isLoading = false;
paginator.current.totalRecords = response.totalRecordsCount;
})
.catch((error) => {
console.error(error.message);
setData([]);
hierarchicalGrid.current.isLoading = false;
})
}
function gridCreated(
rowIsland: IgrRowIsland,
event: IgrGridCreatedEventArgs,
parentKey: string
) {
const context = event.detail;
context.grid.isLoading = true;
const parentId: string = context.parentID;
const childDataKey: string = rowIsland.childDataKey;
RemoteService.getHierarchyDataById(parentKey, parentId, childDataKey)
.then((data: any) => {
context.grid.data = data;
context.grid.isLoading = false;
context.grid.markForCheck();
})
.catch((error) => {
console.error(error.message);
context.grid.data = [];
context.grid.isLoading = false;
context.grid.markForCheck();
})
}
function onPageNumberChange(paginator: IgrPaginator, args: IgrNumberEventArgs) {
setPage(args.detail);
}
function onPageSizeChange(paginator: IgrPaginator, args: IgrNumberEventArgs) {
setPerPage(args.detail);
}
return (
<div className="container sample ig-typography">
<div className="container fill">
<IgrHierarchicalGrid
ref={hierarchicalGrid}
data={data}
pagingMode={GridPagingMode.Remote}
primaryKey="customerId"
height="600px"
>
<IgrPaginator
perPage={perPage}
ref={paginator}
pageChange={onPageNumberChange}
perPageChange={onPageSizeChange}>
</IgrPaginator>
<IgrColumn field="customerId" hidden={true}></IgrColumn>
<IgrColumn field="companyName" header="Company Name"></IgrColumn>
<IgrColumn field="contactName" header="Contact Name"></IgrColumn>
<IgrColumn field="contactTitle" header="Contact Title"></IgrColumn>
<IgrColumn field="address.country" header="Country"></IgrColumn>
<IgrColumn field="address.phone" header="Phone"></IgrColumn>
<IgrRowIsland
childDataKey="Orders"
primaryKey="orderId"
gridCreated={(
rowIsland: IgrRowIsland,
e: IgrGridCreatedEventArgs
) => gridCreated(rowIsland, e, "Customers")}
>
<IgrColumn field="orderId" hidden={true}></IgrColumn>
<IgrColumn field="shipAddress.country" header="Ship Country"></IgrColumn>
<IgrColumn field="shipAddress.city" header="Ship City"></IgrColumn>
<IgrColumn field="shipAddress.street" header="Ship Address"></IgrColumn>
<IgrColumn field="orderDate" header="Order Date" dataType="date"></IgrColumn>
<IgrRowIsland
childDataKey="Details"
primaryKey="productId"
gridCreated={(
rowIsland: IgrRowIsland,
e: IgrGridCreatedEventArgs
) => gridCreated(rowIsland, e, "Orders")}
>
<IgrColumn field="productId" hidden={true}></IgrColumn>
<IgrColumn field="quantity" header="Quantity"></IgrColumn>
<IgrColumn field="unitPrice" header="Unit Price"></IgrColumn>
<IgrColumn field="discount" header="Discount"></IgrColumn>
</IgrRowIsland>
</IgrRowIsland>
</IgrHierarchicalGrid>
</div>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
tsx
このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
既知の問題と制限
- グリッドに
PrimaryKey
が設定されておらず、リモート データ シナリオが有効になっている場合 (ページング、ソート、フィルタリング、スクロール時に、グリッドに表示されるデータを取得するためのリモート サーバーへのリクエストがトリガーされる場合)、データ要求が完了すると、行は次の状態を失います:
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。