Customizing the Grid Pager's DropDown
Environment
Property | Value |
---|---|
Product | Telerik UI for ASP.NET Core Grid |
Version | 2022.2.621 |
Description
I would like to customize the pagination drop-down in the Telerik UI for ASP.NET Core Grid Grid footer to load all pages by default. How can I achieve this?
Solution
To customize the pagination drop-down in the Telerik UI for ASP.NET Core Grid Grid footer to load all pages by default, follow these steps:
- Hide the default pager by adding the following CSS code:
.k-grid-pager {
display: none;
}
- Implement a custom div element that will serve as the custom pager. For example:
<div id="customPager"></div>
-
In the
document.ready
scope, retrieve thedataSource
instance of the Telerik UI for ASP.NET Core Grid Grid. -
Handle the
dataBound
Event of the Grid. -
In the Event handler, Use the
totalPages
method of thedataSource
to get the page count of the Grid. -
Implement a DropDownList using the custom div element from step 2, with items ranging from 1 to the page count obtained in step 4.
-
Attach a
change
event handler to the DropDownList. -
In the event handler, retrieve the current value of the DropDownList and set it as the current page of the Grid using the
page
method of thedataSource
.
Here is an example implementation:
<!-- Custom div element for our pager and hiding the default one -->
<div id="customPager"></div>
<style>
.k-grid-pager {
display: none;
}
</style>
<script>
$(document).ready(function () {
var grid = $("#grid").data("kendoGrid");
// Attach an event handler to the dataBound event
grid.bind("dataBound", function(e) {
var pages = e.sender.dataSource.totalPages();
var ddlData = [];
for (var i = 1; i <= pages; i++) {
ddlData.push(i);
}
// Check if the dropdown is already initialized to avoid re-initializing
var customPager = $("#customPager").data("kendoDropDownList");
if (!customPager) {
$("#customPager").kendoDropDownList({
dataSource: ddlData,
change: onChange
});
} else {
// If already initialized, just update its data
customPager.setDataSource(ddlData);
customPager.refresh();
}
});
});
function onChange() {
var ddl = this;
var currPage = ddl.value();
var grid = $("#grid").data("kendoGrid");
grid.dataSource.page(currPage);
}
</script>