Example of Keyboard Navigation in ASP.NET MVC Data Grid Control
Keyboard shortcuts can be used to interact with DataGrid functionality. In the example below, various key combinations can be used to interact with the grid.
@Html.UiGrid(new UiGridOptions
{
Id = "KeyboardNavigation",
Data = ViewData["dataSource"],
AllowFiltering = true,
AllowSorting = true,
AllowGrouping = true,
Selection = UiGridSelection.Multiple,
PageCount = 5,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Menu,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Normal },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 120, TextAlign = UiTextAlign.Right, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 150, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, AllowGrouping = false, Width = 150, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit }
}
})
<style>
.keydescription {
padding-left: 20px;
}
.keys {
display: inline-block;
width: 230px;
}
</style>
@Html.UiGrid(new UiGridOptions
{
Id = "KeyboardNavigation",
Data = ViewData["dataSource"],
AllowFiltering = true,
AllowSorting = true,
AllowGrouping = true,
Selection = UiGridSelection.Multiple,
PageCount = 5,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Menu,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Normal },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 120, TextAlign = UiTextAlign.Right, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 150, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, AllowGrouping = false, Width = 150, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/KeyboardNavigation.cshtml; it draws what ViewData holds.
public ActionResult KeyboardNavigation()
{
var order = KeyboardOrders.GetAllRecords();
ViewData["datasource"] = order;
return View();
}
public class KeyboardOrders
{
public KeyboardOrders()
{
}
public KeyboardOrders(int OrderID, string CustomerId, int EmployeeId, decimal Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
{
this.OrderID = OrderID;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCity = ShipCity;
this.Verified = Verified;
this.OrderDate = OrderDate;
this.ShipName = ShipName;
this.ShipCountry = ShipCountry;
this.ShippedDate = ShippedDate;
this.ShipAddress = ShipAddress;
}
public static List<KeyboardOrders> GetAllRecords()
{
List<KeyboardOrders> orders = new List<KeyboardOrders>();
int code = 10000;
for (int i = 1; i < 10; i++)
{
orders.Add(new KeyboardOrders(code + 1, "ALFKI", i + 0, 2.32m * i, false, new DateTime(1991, 05, 15), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16), "Kirchgasse 6"));
orders.Add(new KeyboardOrders(code + 2, "ANATR", i + 2, 3.32m * i, true, new DateTime(1990, 04, 04), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11), "Avda. Azteca 123"));
orders.Add(new KeyboardOrders(code + 3, "ANTON", i + 1, 4.35m * i, true, new DateTime(1957, 11, 30), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
orders.Add(new KeyboardOrders(code + 4, "BLONP", i + 3, 5.38m * i, false, new DateTime(1930, 10, 22), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30), "Magazinweg 7"));
orders.Add(new KeyboardOrders(code + 5, "BOLID", i + 4, 6.35m * i, true, new DateTime(1953, 02, 18), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3), "1029 - 12th Ave. S."));
code += 5;
}
return orders;
}
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public int? EmployeeID { get; set; }
public decimal? Freight { get; set; }
public string ShipCity { get; set; }
public bool Verified { get; set; }
public DateTime OrderDate { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
public DateTime ShippedDate { get; set; }
public string ShipAddress { get; set; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The key combinations listed below can be used in the grid to initiate various actions.
- FOCUS ELEMENTS
- Home- Moves the focus to the first cell of the focused row.
- End- Moves the focus to the last cell of the focused row.
- Ctrl + Home- Moves the focus to the first Cell of the first row in our DataGrid component.
- Ctrl+End- Moves the focus to the last Cell of the last row in our DataGrid component.
- Down arrow- Moves the cell focus downward from the focused cell.
- Up arrow- Moves the cell focus upward from the focused cell.
- Right arrow / Tab- Moves the cell focus right side from the focused cell.
- Left arrow / Shift+Tab- Moves the cell focus left side from the focused cell.
- Alt+J- Moves the focus to the entire grid.
- Alt+W- Move the focus to the grid content element
- Page down / Right arrow- Navigates to the next page.
- Page up / Left arrow- Navigates to the previous page.
- Enter / Space- Select the currently focused page.
- Tab- Focus on the next pager item.
- Shift+Tab- Focus on the previous pager item.
- Home / Ctrl+Alt+Page up- Navigates to the first page.
- End / Ctrl+Alt+Page down- Navigates to the last page.
- Shift+Down arrow- Extends the row/cell selection downwards from the selected row/cell.
- Shift+Up arrow- Extends the row/cell selection upwards from the selected row/cell.
- Shift+Left arrow- Extends the cell selection to the left side from the selected cell.
- Shift+Right arrow- Extends the cell selection to the right side from the selected cell.
- Enter- Moves the row/cell selection downward from the selected cell/row
- Esc- Deselects all the selected row/cells.
- Ctrl+A- Select all the row/cells in the current page.
- Up arrow- Moves up a row/cell selection from the selected row/cell.
- Down arrow- Moves down a row/cell selection from the selected row/cell.
- Right arrow- Moves to the right cell selection from the selected cell.
- Left arrow- Moves to the left cell selection from the selected cell.
- F2- Starts editing of selected row if Mode is Normal/Dialog or Starts editing of selected cell if Mode is Batch.
- Enter- Saves the current form it the Mode is Normal or Dialog / Saves the current cell and starts editing the next row cell if Mode is Batch.
- Del- Deletes the current selected record.
- Insert- Creates a new add form depending on the NewRowPosition.
- Tab- Navigates to the next editable cell if the Mode is Normal or Dialog / Saves the current cell and starts editing the next cell is Mode is Batch.
- Shift+Tab- Navigates to the previous editable cell if the Mode is Normal or Dialog / Saves the current cell and starts editing the previous cell is Mode is Batch
- Shift+Enter- Saves the current cell and starts editing the previous row cell if Mode is Batch.
- Alt+Down arrow- Opens the filter menu(excel, menu and checkbox filter) when its header element is in focused state.
- Enter- Performs sorting (ascending/descending) on a column when its header element is in focused state.
- Ctrl+Enter- Performs multi-sorting on a column when its header element is in focused state.
- Shift+Enter- Clears sorting for the focused header column.
- Ctrl+Down arrow- Expands all the Visible Group.
- Ctrl+Up arrow- Collapses all the visible groups.
- Ctrl+Space- Performs grouping when focused on a header element.
- Enter- If the current cell is an expand/collapse cell then expands/collapses the current group/detailrow/childgrid.