New to Kendo UI for jQuery? Download free 30-day trial

Dynamically Change the Page Size of the Grid to Adjust Its Height

Environment

Product Progress® Kendo UI® Grid for jQuery
Operating System All
Browser All
Browser Version All

Description

How can I change the pageSize property of the Grid when the height changes so that as many rows as possible are always displayed.

Solution

  1. Calculate the number of rows that will fit in the available space by subscribing to the resize event of the window element.
  2. Change the pageSize by using the pageSize method of the dataSource.

This solution adjusts the pageSize of the Grid dataSource only when the window is being resized. Initially the Grid will display as many records as it is specified in the pageSize declaration.

<style>
  html,
  body,
  #parent,
  #grid {
    margin: 0;
    padding: 0;
    border-width: 0;
    height: 100%;
    /* DO NOT USE !important for setting the Grid height! */
  }

  html {
    overflow: hidden;
    font: 13px sans-serif;
  }
</style>
<div id="parent">
  <div id="grid"></div>
</div>
<script>
  var gridElement = $("#grid");

  function resizeGrid() {
    gridElement.data("kendoGrid").resize();

    var newHeight = gridElement.height();

    var headerHeight = $(".k-grid-header").height();
    var pagerHeight = $(".k-grid-pager").height();
    var rowHeight = $("#grid tr:last").height();

    var numberOfRows = Math.round((newHeight - headerHeight - pagerHeight) / rowHeight);

    gridElement.data("kendoGrid").dataSource.pageSize(numberOfRows);
    gridElement.data("kendoGrid").refresh();
  }

  $(window).resize(function() {
    resizeGrid();
  });

  $("#grid").kendoGrid({
    dataSource: {
      type: "odata",
      transport: {
        read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
      },
      schema: {
        model: {
          fields: {
            OrderID: {
              type: "number"
            },
            ShipName: {
              type: "string"
            },
            ShipCity: {
              type: "string"
            }
          }
        }
      },
      pageSize: 25
    },
    scrollable: false,
    resizable: true,
    pageable: true,
    columns: [{
        field: "OrderID",
        filterable: false,
        width: 200
      },
      "ShipName",
      "ShipCity"
    ]
  });
</script>

See Also

In this article