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 e-page-settings tag helper.

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

tagHelper
page.cs
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowPaging="true" height="325" load="load"> 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
   function load() {
        var grid = document.getElementById('Grid').ej2_instances[0];
        var rowHeight = grid.getRowHeight();  //height of the each row
        var gridHeight = grid.height;  //grid height
        var pageSize = grid.pageSettings.pageSize;   //initial page size
        var pageResize = (gridHeight - (pageSize * rowHeight)) / rowHeight; //new page size is obtained here
        grid.pageSettings.pageSize = pageSize + Math.round(pageResize);
   }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

You can achieve better performance by using grid paging to fetch only a pre-defined number of records from the data source.

Template

You can use custom elements inside the pager instead of default elements. The custom elements can be defined by using the template property of e-page-settings tag helper. Inside this template, you can access the CurrentPage, PageSize, TotalPage and TotalRecordCount values.

tagHelper
page-temp.cs
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" dataBound="dataBound" actionComplete="actionComplete" allowPaging="true">
    <e-grid-pagesettings template="#template" pageSize="7">
    </e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function updateTemplate() {
        var numeric;
        var grid = document.getElementById("Grid").ej2_instances[0];
        numeric = new ej.inputs.NumericTextBox({
            min: 1,
            max: 3,
            step: 1,
            format: '###.##',
            change: function (args) {
                let value = args.value;
                grid.goToPage(value);
            }
        });
        numeric.appendTo('#currentPage');
    };
    var flag = true;
    function dataBound() {
        if (flag) {
            flag = false;
            updateTemplate();
        }
    }
    function actionComplete(args) {
        if (args.requestType === 'paging') {
            updateTemplate();
        }
    }
</script>

<script id="template" type="text/x-template">
    <div class="e-pagertemplate">
        <div class="col-lg-12 control-section">
            <div class="content-wrapper">
                <input id="currentPage" type="text" value=${currentPage}>
            </div>
        </div>

        <div id="totalPages" class="e-pagertemplatemessage" style="margin-top:5px;margin-left:30px;border: none; display: inline-block ">
            <span class="e-pagenomsg">${currentPage} of ${totalPages} pages (${totalRecordsCount} items)</span>
        </div>
    </div>
</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

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 pageSizes property of e-page-settings as true.

tagHelper
pagerdropdown.cs
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowPaging="true"> 
    <e-grid-pagesettings pageSizes="true">
    </e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

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.

tagHelper
pagerattop.cs
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" dataBound="dataBound" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update","Cancel" })" allowPaging="true">
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    var initialLoad = true;
    function dataBound(args) {
        if (initialLoad) {
            initialLoad = false;
            var pager = document.getElementsByClassName('e-gridpager');
            var topElement;
            if (this.toolbar) {
                topElement = document.getElementsByClassName('e-toolbar');
            } else {
                topElement = document.getElementsByClassName('e-gridheader');
            }
            topElement[0].before(pager[0]);
        }
    };
</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

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