Skip to content

Commit c7eb10d

Browse files
Merge pull request #2 from DevExpress-Examples/doc_review
UX improvements and code simplification
2 parents fb3c356 + bc0a26f commit c7eb10d

File tree

9 files changed

+62
-130
lines changed

9 files changed

+62
-130
lines changed

CS/GridCustomShortcuts/Components/Layout/NavMenu.razor

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<DxMenu Orientation="@Orientation.Vertical" CssClass="menu">
33
<Items>
44
<DxMenuItem NavigateUrl="/" Text="Home" CssClass="menu-item" IconCssClass="icon home-icon"></DxMenuItem>
5-
<DxMenuItem NavigateUrl="/counter" Text="Counter" CssClass="menu-item" IconCssClass="icon counter-icon"></DxMenuItem>
6-
<DxMenuItem NavigateUrl="/weather" Text="Weather" CssClass="menu-item" IconCssClass="icon weather-icon"></DxMenuItem>
75
</Items>
86
</DxMenu>
97
</div>

CS/GridCustomShortcuts/Components/Pages/Counter.razor

Lines changed: 0 additions & 27 deletions
This file was deleted.

CS/GridCustomShortcuts/Components/Pages/Counter.razor.css

Lines changed: 0 additions & 37 deletions
This file was deleted.

CS/GridCustomShortcuts/Components/Pages/Index.razor

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
@inject WeatherForecastService ForecastService
77
@implements IAsyncDisposable
88

9+
<PageTitle>Blazor Grid Keyboard Shortcuts</PageTitle>
10+
<h1>Weather Forecast</h1>
11+
912
@if (forecasts == null)
1013
{
1114
<p><em>Loading...</em></p>
1215
}
1316
else
1417
{
1518
<div @ref="gridWrapper">
16-
<DxGrid @ref="grid" Data="@forecasts"
17-
FocusedRowEnabled="true"
18-
EditMode="GridEditMode.EditCell"
19-
EditModelSaving="OnEditModelSaving"
20-
CssClass="mw-1100" ValidationEnabled=false>
19+
<DxGrid @ref="grid" Data="@forecasts"
20+
FocusedRowEnabled="true"
21+
EditModelSaving="OnEditModelSaving"
22+
CssClass="mw-1100">
2123
<Columns>
22-
<DxGridSelectionColumn />
24+
<DxGridSelectionColumn Width="70px" />
2325
<DxGridDataColumn Caption="Date" FieldName="Date" />
2426
<DxGridDataColumn Caption="Temperature (C)" FieldName="TemperatureC" />
2527
<DxGridDataColumn Caption="Temperature (F)" FieldName="TemperatureF" />
@@ -28,18 +30,18 @@ else
2830
</DxGrid>
2931
</div>
3032

31-
32-
<DxPopup HeaderText="Forecast Entry"
33-
@bind-Visible="@popupVisible"
34-
BodyText="@("Viewing forecast for " + currentDate)">
33+
<DxPopup HeaderText="@($"Weather Forecast for {selectedForecast?.Date:m}")"
34+
@bind-Visible="@popupVisible">
35+
<p><b>Temperature:</b> @selectedForecast?.TemperatureC °C</p>
36+
<p><b>Summary:</b> @selectedForecast?.Summary</p>
3537
</DxPopup>
3638
}
3739

3840
@code {
3941
private WeatherForecast[]? forecasts;
4042
private bool popupVisible;
4143
private ElementReference gridWrapper;
42-
private DateOnly currentDate;
44+
private WeatherForecast? selectedForecast;
4345
private IGrid grid;
4446
private IJSObjectReference? module;
4547
private DotNetObjectReference<Index>? dotNetHelper;
@@ -51,7 +53,7 @@ else
5153
if (focusedItem == null)
5254
return;
5355
await grid.SaveChangesAsync();
54-
currentDate = focusedItem.Date;
56+
selectedForecast = focusedItem;
5557
popupVisible = true;
5658
StateHasChanged();
5759
}
@@ -74,6 +76,7 @@ else
7476
module = await JS.InvokeAsync<IJSObjectReference>("import",
7577
"./Components/Pages/Index.razor.js");
7678
dotNetHelper = DotNetObjectReference.Create(this);
79+
await module.InvokeVoidAsync("focusGrid");
7780
await module.InvokeVoidAsync("addCaptureKeyListener", gridWrapper, dotNetHelper);
7881
}
7982
}

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
}

CS/GridCustomShortcuts/Components/Pages/Weather.razor

Lines changed: 0 additions & 35 deletions
This file was deleted.

README.md

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
11
<!-- default badges list -->
2-
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/970685143/24.2.1%2B)
32
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1290517)
43
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
54
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
65
<!-- default badges end -->
7-
# Product/Platform - Task
6+
# Blazor Grid - Custom Keyboard Shortcuts
87

9-
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.
109

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

13-
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+
![Blazor Grid Custom Keyboard Shortcuts](images/grid-keyboard-shortcuts.gif)
16+
17+
> [!NOTE]
18+
> For keyboard shortcuts to operate, the Grid component must be focused. To bring the grid into focus, click anywhere within its area.
19+
20+
## Implementation Details
21+
22+
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>.
23+
24+
Create a [JavaScript file](CS/GridCustomShortcuts/Components/Pages/Index.razor.js) in the project. This file implements the following functions for managing keyboard shortcuts:
25+
26+
- `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.
27+
- `removeCaptureKeyListener` - Removes the previously attached keyboard event listener.
28+
- `focusGrid` - Focus the grid programmatically on page load. This ensures shortcuts are instantly available.
29+
30+
### Bind JavaScript with .NET Code
31+
32+
In the `@code` section of the [Index.razor](CS/GridCustomShortcuts/Components/Pages/Index.razor) page:
33+
34+
1. Register the JavaScript code in `OnAfterRenderAsync` lifecycle method.
35+
2. Call the `focusGrid` JavaScript function.
36+
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.)
37+
4. Implement `SelectAllRows` and `HandleKeyDown` JSInvokable methods to handle operations triggered by keyboard shortcuts.
38+
39+
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.
40+
41+
### Release Resources
42+
43+
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.
44+
45+
For technical details, read [Implement a DisposeAsync method](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync) article.
1446

1547
## Files to Review
1648

17-
- link.cs (VB: link.vb)
18-
- link.js
19-
- ...
49+
- [Index.razor](CS/GridCustomShortcuts/Components/Pages/Index.razor)
50+
- [Index.razor.js](CS/GridCustomShortcuts/Components/Pages/Index.razor.js)
2051

2152
## Documentation
2253

23-
- link
24-
- link
25-
- ...
26-
27-
## More Examples
54+
- [Keyboard Support in Blazor Grid](https://docs.devexpress.com/Blazor/404652/components/grid/keyboard-support)
55+
- [DxGrid Class](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid)
56+
- [DxPopup Class](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxPopup)
2857

29-
- link
30-
- link
31-
- ...
3258
<!-- feedback -->
3359
## Does this example address your development requirements/objectives?
3460

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"autoGenerateVb": true,
2+
"autoGenerateVb": false,
33
"runOnWeb": false
44
}

images/grid-keyboard-shortcuts.gif

1.95 MB
Loading

0 commit comments

Comments
 (0)