Search results

Paging

Paging provides an option to display Grid data in page segments. To enable paging, set the allowPaging to true. When paging is enabled, pager component renders at the bottom of the grid. Paging options can be configured through the pageSettings.

In the below sample, pageSize is calculated based on the grid height by using the load event.

To use Paging, inject Page module in Grid.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Page } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
    private gridInstance: GridComponent;
    public onLoad() {
        let rowHeight: number = this.gridInstance.getRowHeight();  //height of the each row
        let gridHeight: number = this.gridInstance.height;  //grid height
        let pageSize: number = this.gridInstance.pageSettings.pageSize;   //initial page size
        let pageResize: any = (gridHeight - (pageSize * rowHeight)) / rowHeight; //new page size is obtained here
        this.gridInstance.pageSettings.pageSize = pageSize + Math.round(pageResize);
    }

    render(){
        return <GridComponent  dataSource={data} ref={grid => this.gridInstance = grid} allowPaging={true} height={325} load={this.onLoad.bind(this)}>
                  <ColumnsDirective>
                    <ColumnDirective field='OrderID' headerText='Order ID' width='120' textAlign="Right"></ColumnDirective>
                    <ColumnDirective field='CustomerID' headerText='Customer ID' width='150'></ColumnDirective>
                    <ColumnDirective field='ShipCity' headerText='Ship City' width='150'></ColumnDirective>
                    <ColumnDirective field='ShipName' headerText='Ship Name' width='150'></ColumnDirective>
                </ColumnsDirective>
                <Inject services={[Page]}/>
            </GridComponent>
        }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Grid Paging enables you to achieve better performance by fetching only a predefined number of records from the data source.

Pager with Page Size Dropdown

The pager Dropdown allows you to change the number of records in the Grid dynamically. It can be enabled by defining the pageSettings.pageSizes property as true.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Page, PageSettingsModel} from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
    public pageOptions: PageSettingsModel = { pageSize: 8, pageSizes: true };
    render(){
        return <GridComponent  dataSource={data} allowPaging={true} height={268} pageSettings={this.pageOptions}>
                  <ColumnsDirective>
                    <ColumnDirective field='OrderID' headerText='Order ID' width='120' textAlign="Right"></ColumnDirective>
                    <ColumnDirective field='CustomerID' headerText='Customer ID' width='150'></ColumnDirective>
                    <ColumnDirective field='ShipCity' headerText='Ship City' width='150'></ColumnDirective>
                    <ColumnDirective field='ShipName' headerText='Ship Name' width='150'></ColumnDirective>
                </ColumnsDirective>
                <Inject services={[Page]}/>
            </GridComponent>
        }
};
ReactDOM.render(<App />, document.getElementById('grid'));

How to render Pager at the Top of the Grid

By default, Pager will be rendered at the bottom of the Grid. You can also render the Pager at the top of the Grid by using the dataBound event.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Page, Toolbar, ToolbarItems, PageSettingsModel} from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
    private gridInstance: GridComponent;
    public toolbarOptions: ToolbarItems[] = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
    public initialGridLoad: boolean = true;
    public pageOptions: PageSettingsModel = { pageSize: 8, pageSizes: true };
    public dataBound() {
        if (this.initialGridLoad) {
            this.initialGridLoad = false;
            let pager: any = document.getElementsByClassName('e-gridpager');
            let topElement: any;
            if (this.gridInstance.allowGrouping || this.gridInstance.toolbar) {
                topElement = this.gridInstance.allowGrouping ? document.getElementsByClassName('e-groupdroparea') :
                            document.getElementsByClassName('e-toolbar');
            } else {
                topElement = document.getElementsByClassName('e-gridheader');
            }
            this.gridInstance.element.insertBefore(pager[0], topElement[0]);
        }
    }
    render(){
        return <GridComponent  dataSource={data} ref={grid => this.gridInstance = grid} toolbar={this.toolbarOptions} allowPaging={true} height={268} pageSettings={this.pageOptions} dataBound={this.dataBound.bind(this)}>
                  <ColumnsDirective>
                    <ColumnDirective field='OrderID' headerText='Order ID' width='120' textAlign="Right"></ColumnDirective>
                    <ColumnDirective field='CustomerID' headerText='Customer ID' width='150'></ColumnDirective>
                    <ColumnDirective field='ShipCity' headerText='Ship City' width='150'></ColumnDirective>
                    <ColumnDirective field='ShipName' headerText='Ship Name' width='150'></ColumnDirective>
                </ColumnsDirective>
                <Inject services={[Page, Toolbar]}/>
            </GridComponent>
        }
};
ReactDOM.render(<App />, document.getElementById('grid'));

During the paging action, the pager component triggers the below three events.

  • The created event triggers when Pager is created.
  • The click event triggers when the numeric items in the pager is clicked.
  • The dropDownChanged event triggers when pageSize DropDownList value is selected.

See Also