@@ -3,77 +3,73 @@ import React, { useEffect, useRef, useState } from "react";
33import ReactDOM from "react-dom/client" ;
44import "./index.css" ;
55
6- import { IgrGrid , IgrPaginator , IgrGridModule } from "igniteui-react-grids" ;
6+ import { IgrGrid , IgrPaginator , IgrGridModule , GridPagingMode } from "igniteui-react-grids" ;
77import { IgrColumn } from "igniteui-react-grids" ;
88
99import "igniteui-react-grids/grids/combined" ;
1010import "igniteui-react-grids/grids/themes/light/bootstrap.css" ;
1111import { RemoteService } from "./RemotePagingService" ;
12+ import { CustomersWithPageResponseModel } from "./CustomersWithPageResponseModel" ;
13+ import { IgrNumberEventArgs } from "igniteui-react" ;
1214
1315IgrGridModule . register ( ) ;
1416
1517export default function App ( ) {
16- let data = [ ] ;
17- const grid = useRef < IgrGrid > ( null ) ;
18- const paginator = useRef < IgrPaginator > ( null ) ;
19- const remoteServiceInstance = new RemoteService ( ) ;
20- let [ page ] = useState ( 0 ) ;
21- let [ perPage , setPerPage ] = useState ( 15 ) ;
18+ const grid = useRef < IgrGrid > ( null ) ;
19+ const paginator = useRef < IgrPaginator > ( null ) ;
20+ const [ data , setData ] = useState ( [ ] ) ;
21+ const [ page , setPage ] = useState ( 0 ) ;
22+ const [ perPage , setPerPage ] = useState ( 15 ) ;
2223
23- useEffect ( ( ) => {
24- if ( paginator . current ) {
25- setPerPage ( 15 ) ;
26- grid . current . isLoading = true ;
27- }
28-
29- grid . current . isLoading = true ;
30- loadData ( 'Customers' ) ;
31- } , [ page , 15 ] ) ;
32-
33- function loadData ( dataKey : string ) {
34- const dataState = { key : dataKey } ;
24+ useEffect ( ( ) => {
25+ loadGridData ( page , perPage ) ;
26+ } , [ page , perPage ] ) ;
3527
36- // Set loading state
28+ function loadGridData ( pageIndex ?: number , pageSize ?: number ) {
29+ // Set loading
3730 grid . current . isLoading = true ;
3831
39- // Fetch data length
40- remoteServiceInstance . getDataLength ( dataState ) . then ( ( length : number ) => {
41- paginator . current . totalRecords = length ;
42- } ) ;
43-
4432 // Fetch data
45- remoteServiceInstance . getData ( dataState ) . then ( ( data : any [ ] ) => {
46- grid . current . isLoading = false ;
47- grid . current . data = data ;
48- grid . current . markForCheck ( ) ;
49- } ) ;
33+ RemoteService . getDataWithPaging ( pageIndex , pageSize )
34+ . then ( ( response : CustomersWithPageResponseModel ) => {
35+ setData ( response . items ) ;
36+ // Stop loading when data is retrieved
37+ grid . current . isLoading = false ;
38+ paginator . current . totalRecords = response . totalRecordsCount ;
39+ } )
40+ . catch ( ( error ) => {
41+ console . error ( error . message ) ;
42+ setData ( [ ] ) ;
43+ // Stop loading even if error occurs. Prevents endless loading
44+ grid . current . isLoading = false ;
45+
46+ } )
5047 }
5148
52- function paginate ( pageArgs : number ) {
53- page = pageArgs ;
54- const skip = page * perPage ;
55- const top = perPage ;
49+ function onPageNumberChange ( paginator : IgrPaginator , args : IgrNumberEventArgs ) {
50+ setPage ( args . detail ) ;
51+ }
5652
57- remoteServiceInstance . getData ( { key : 'Customers' } , skip , top ) . then ( ( incData :any ) => {
58- data = incData ;
59- grid . current . isLoading = false ;
60- grid . current . markForCheck ( ) ; // Update the UI after receiving data
61- } ) ;
62- }
53+ function onPageSizeChange ( paginator : IgrPaginator , args : IgrNumberEventArgs ) {
54+ setPerPage ( args . detail ) ;
55+ }
6356
6457 return (
6558 < div className = "container sample ig-typography" >
6659 < div className = "container fill" >
6760 < IgrGrid
6861 ref = { grid }
62+ data = { data }
63+ pagingMode = { GridPagingMode . Remote }
6964 primaryKey = "customerId"
7065 height = "600px"
7166 >
7267 < IgrPaginator
73- perPage = "15"
68+ perPage = { perPage }
7469 ref = { paginator }
75- pageChange = { ( evt : { page : number } ) => paginate ( evt . page ) }
76- perPageChange = { ( ) => paginate ( 0 ) } > </ IgrPaginator >
70+ pageChange = { onPageNumberChange }
71+ perPageChange = { onPageSizeChange } >
72+ </ IgrPaginator >
7773 < IgrColumn field = "customerId" hidden = { true } > </ IgrColumn >
7874 < IgrColumn field = "companyName" header = "Company Name" > </ IgrColumn >
7975 < IgrColumn field = "contactName" header = "Contact Name" > </ IgrColumn >
0 commit comments