Skip to content

Commit ca9d8a1

Browse files
committed
Readme
1 parent b9ae311 commit ca9d8a1

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

CS/GridCustomShortcuts/Components/Pages/Index.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ else
7676
module = await JS.InvokeAsync<IJSObjectReference>("import",
7777
"./Components/Pages/Index.razor.js");
7878
dotNetHelper = DotNetObjectReference.Create(this);
79+
await module.InvokeVoidAsync("focusGrid");
7980
await module.InvokeVoidAsync("addCaptureKeyListener", gridWrapper, dotNetHelper);
8081
}
8182
}

CS/GridCustomShortcuts/Components/Pages/Index.razor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ export function removeCaptureKeyListener() {
2323
window.removeEventListener('keydown', keydownHandler, true);
2424
keydownHandler = null;
2525
}
26+
}
27+
28+
export function focusGrid() {
29+
document.getElementsByClassName('dxbl-grid')[0].focus();
2630
}

README.md

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,56 @@
33
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
44
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
55
<!-- default badges end -->
6-
# Product/Platform - Task
6+
# Blazor Grid - Custom Keyboard Shortcuts
77

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.
99

10-
Put a screenshot that illustrates the result here.
10+
This example binds custom keyboard shortcuts to the Grid component:
1111

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.
1344

1445
## Files to Review
1546

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)
1949

2050
## Documentation
2151

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)
2755

28-
- link
29-
- link
30-
- ...
3156
<!-- feedback -->
3257
## Does this example address your development requirements/objectives?
3358

0 commit comments

Comments
 (0)