|
3 | 3 | [](https://docs.devexpress.com/GeneralInformation/403183) |
4 | 4 | [](#does-this-example-address-your-development-requirementsobjectives) |
5 | 5 | <!-- default badges end --> |
6 | | -# Product/Platform - Task |
| 6 | +# Blazor Grid - Custom Keyboard Shortcuts |
7 | 7 |
|
8 | | -This is the repository template for creating new examples. Describe the solved task here. |
| 8 | +The DevExpress Blazor [Grid](https://docs.devexpress.com/Blazor/403143/components/grid) supports a number of keyboard shortcuts out-of-the-box. You can also define custom key combinations to further enhance interaction speed and streamline workflows according to business requirements or user preferences. |
9 | 9 |
|
10 | | -Put a screenshot that illustrates the result here. |
| 10 | +This example binds custom keyboard shortcuts to the Grid component: |
11 | 11 |
|
12 | | -Then, add implementation details (steps, code snippets, and other technical information in a free form), or add a link to an existing document with implementation details. |
| 12 | +- <kbd>Ctrl</kbd> + <kbd>A</kbd>: select all rows |
| 13 | +- <kbd>Shift</kbd> + <kbd>Enter</kbd>: open row details |
| 14 | + |
| 15 | +> **Note**: |
| 16 | +> For keyboard shortcuts to operate, the Grid component must be focused. To bring the grid into focus, click anywhere within its area. |
| 17 | +
|
| 18 | +## Implementation Details |
| 19 | + |
| 20 | +Add [DxGrid](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid) and [DxPopup](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxPopup) components to the [page](CS/GridCustomShortcuts/Components/Pages/Index.razor). The popup will display the selected cell's details when a user presses <kbd>Shift</kbd> + <kbd>Enter</kbd>. |
| 21 | + |
| 22 | +Create a [JavaScript file](CS/GridCustomShortcuts/Components/Pages/Index.razor.js) in the project. This file implements the following functions for managing keyboard shortcuts: |
| 23 | + |
| 24 | +- `addCaptureKeyListener` - Attaches a keyboard event listener to the grid and defines custom shortcuts. To prevent default web browser actions for the same key combinations, call `event.stopPropagation();` within the event handler. |
| 25 | +- `removeCaptureKeyListener` - Removes the previously attached keyboard event listener. |
| 26 | +- `focusGrid` - Focus the grid programmatically on page load. This ensures shortcuts are instantly available. |
| 27 | + |
| 28 | +### Bind JavaScript with .NET Code |
| 29 | + |
| 30 | +In the `@code` section of the [Index.razor](CS/GridCustomShortcuts/Components/Pages/Index.razor) page: |
| 31 | + |
| 32 | +1. Register the JavaScript code in `OnAfterRenderAsync` lifecycle method. |
| 33 | +2. Call the `focusGrid` JavaScript function. |
| 34 | +3. Call the `addCaptureKeyListener` JavaScript function. Pass the `DotNetObjectReference` (for interoperability from JavaScript to .NET) and a reference to the `<div>` element that surrounds the grid (for capturing keyboard events.) |
| 35 | +4. Implement `SelectAllRows` and `HandleKeyDown` JSInvokable methods to handle operations triggered by keyboard shortcuts. |
| 36 | + |
| 37 | +For technical details, read [Call .NET methods from JavaScript functions in ASP.NET Core Blazor](https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript) article. |
| 38 | + |
| 39 | +### Release Resources |
| 40 | + |
| 41 | +In the `@code` section of the [Index.razor](CS/GridCustomShortcuts/Components/Pages/Index.razor) page, implement a `DisposeAsync` method. It removes the keyboard event listener, cleans up JavaScript resources, and frees allocated memory. |
| 42 | + |
| 43 | +For technical details, read [Implement a DisposeAsync method](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync) article. |
13 | 44 |
|
14 | 45 | ## Files to Review |
15 | 46 |
|
16 | | -- link.cs (VB: link.vb) |
17 | | -- link.js |
18 | | -- ... |
| 47 | +- [Index.razor](CS/GridCustomShortcuts/Components/Pages/Index.razor) |
| 48 | +- [Index.razor.js](CS/GridCustomShortcuts/Components/Pages/Index.razor.js) |
19 | 49 |
|
20 | 50 | ## Documentation |
21 | 51 |
|
22 | | -- link |
23 | | -- link |
24 | | -- ... |
25 | | - |
26 | | -## More Examples |
| 52 | +- [Keyboard Support in Blazor Grid](https://docs.devexpress.com/Blazor/404652/components/grid/keyboard-support) |
| 53 | +- [DxGrid Class](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid) |
| 54 | +- [DxPopup Class](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxPopup) |
27 | 55 |
|
28 | | -- link |
29 | | -- link |
30 | | -- ... |
31 | 56 | <!-- feedback --> |
32 | 57 | ## Does this example address your development requirements/objectives? |
33 | 58 |
|
|
0 commit comments