React Hierarchical Grid のリモート データ操作
デフォルトで、IgrHierarchicalGrid は独自のロジックを使用してデータ操作を実行します。
これらのタスクをリモートで実行し、IgrHierarchicalGrid で公開される特定の入力とイベントを使用して IgrHierarchicalGrid に結果のデータを供給できます。
リモート ページング
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}`;
}
// Add pageIndex and size to the query string if they are defined
if (pageIndex !== undefined) {
qS += `?pageIndex=${pageIndex}`;
if (pageSize !== undefined) {
qS += `&size=${pageSize}`;
}
} else if (pageSize !== undefined) {
qS += `?perPage=${pageSize}`;
}
return `${qS}`;
}
}
サービスを宣言した後にコンポーネントを作成する必要があり、IgrHierarchicalGrid コンストラクションとデータ サブスクリプションを処理します。
<IgrHierarchicalGrid
ref={hierarchicalGrid}
data={data}
pagingMode="remote"
primaryKey="customerId"
height="600px"
>
<IgrPaginator
perPage={perPage}
ref={paginator}
onPageChange={onPageNumberChange}
onPerPageChange={onPageSizeChange}
></IgrPaginator>
...
<IgrRowIsland
childDataKey="Orders"
primaryKey="orderId"
onGridCreated={onCustomersGridCreatedHandler}>
...
<IgrRowIsland
childDataKey="Details"
primaryKey="productId"
onGridCreated={onOrdersGridCreatedHandler}>
...
</IgrRowIsland>
</IgrRowIsland>
</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]);
次に、データを読み込むためのメソッドを設定します。
function loadGridData(pageIndex?: number, pageSize?: number) {
// Set loading state
setIsLoading(true);
// Fetch data
RemoteService.getCustomersDataWithPaging(pageIndex, pageSize)
.then((response: CustomersWithPageResponseModel) => {
setData(response.items);
// Stop loading when data is retrieved
setIsLoading(false);
paginator.current.totalRecords = response.totalRecordsCount;
})
.catch((error) => {
console.error(error.message);
setData([]);
// Stop loading even if error occurs. Prevents endless loading
setIsLoading(false);
})
}
最後に、RowIslands の動作を設定します。
function gridCreated(event: IgrGridCreatedEventArgs, parentKey: string) {
const context = event.detail;
context.grid.isLoading = true;
const parentId: string = context.parentID;
const childDataKey: string = context.owner.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();
});
}
const onCustomersGridCreatedHandler = (e: IgrGridCreatedEventArgs) => {
gridCreated(e, "Customers")
};
const onOrdersGridCreatedHandler = (e: IgrGridCreatedEventArgs) => {
gridCreated(e, "Orders")
};
詳細については、以下の完全なサンプルをご覧ください。
グリッド リモート ページングのデモ
既知の問題と制限
グリッドに
primaryKeyが設定されておらず、リモート データ シナリオが有効になっている場合 (ページング、ソート、フィルタリング、スクロール時に、グリッドに表示されるデータを取得するためのリモート サーバーへのリクエストがトリガーされる場合)、データ要求が完了すると、行は次の状態を失います:行の選択
行の展開/縮小
行の編集
行のピン固定
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。