Category / Section
How to place dropdown in LightSwitch Grid`s pager to control page size?
2 mins read
You can place a dropdown in the Grid pager using pager template feature along with the default pager and on change event of the dropdown, change the page size through set model of Grid.
Initially, append some li elements in the div tag and assign them as a targeID for the ejDropDownList and also include the input element in the e-pagercontainer.
<div id="pagesize"> <ul> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> </ul> </div> <script type="text/x-jsrender" id="template"> <div class="e-pagercontainer"> <input type="text" id="pager" /> </div> </script> <div id="Grid"></div>
<script type="text/javascript"> $(function () { $("#Grid").ejGrid({ dataSource: window.gridData, allowPaging: true, dataBound: 'dataBound', pageSettings: { enableTemplates: true, template: "#template", showDefaults: true }, columns: [ { field: "OrderID" }, { field: "CustomerID" }, { field: "EmployeeID" }, { field: "Freight", format: "{0:C}" }, { field: "ShipCountry" } ] }); }); </script>
ASPX
<ej:Grid ID="Grid" runat="server" AllowPaging="True"> <PageSettings ShowDefaults="true" EnableTemplates="true" Template="#template" /> <Columns> <ej:Column Field="OrderID" /> <ej:Column Field="CustomerID" /> <ej:Column Field="EmployeeID" /> <ej:Column Field="Freight" Format="{0:C}" /> <ej:Column Field=" ShipCountry" /> </Columns> </ej:Grid>
Code behind
public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { this.Grid.DataSource = OrderRespository.GetAllRecords().ToList(); } }
MVC Razor
@(Html.EJ().Grid<OrdersView>("Grid") .Datasource((IEnumerable<object>)ViewBag.dataSource) .AllowPaging() .PageSettings(page => page.EnableTemplates().Template("#template").ShowDefaults()) .Columns(col => { col.Field("OrderID").Add(); col.Field("CustomerID").Add(); col.Field("EmployeeID").Add(); col.Field("Freight").Format("{0:C}").Add(); col.Field("ShipCountry").Add(); }) .ClientSideEvents(events => { events.DataBound("dataBound"); }) )
Controller
namespace MvcApplication66.Controllers { public class HomeController : Controller { public ActionResult Index() { var data = new NorthwindDataContext().OrdersViews.ToList(); ViewBag.dataSource = data; return View(); } } }
Within the dataBound event render the DropDownList and bind change event. In the change event, update the pageSize of the Grid through set model as follows.
<script type="text/javascript"> function dataBound(args) { $('#pager').ejDropDownList({ change: "change", targetID: "pagesize" }); } function change(args) { var gridObj = $("#Grid").ejGrid("instance"); gridObj.option({ "pageSettings": { pageSize: parseInt(args.text) } }); gridObj.getPager().find("input").ejDropDownList({ selectedIndex: args.itemId, change: "change", dataSource: pagerData }); } </script>
The following screenshot will be displayed as the result of the above code example.
Figure: Dropdown in the Grid’s pager using pager template.