Skip to content

Commit 1bb6b4e

Browse files
984421: Updated the UG content and samples for Context-Menu in DataGrid
1 parent 17c7cb2 commit 1bb6b4e

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

blazor/datagrid/context-menu.md

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ documentation: ug
99

1010
# Context menu in Syncfusion Blazor DataGrid
1111

12-
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid supports a context menu that appears when right-clicking any part of the Grid. This menu provides quick access to actions and operations related to the Grid’s data.
12+
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid supports a context menu that appears when right-clicking anywhere within the grid. This menu provides quick access to actions related to the grid’s data and layout, enhancing interactivity and usability.
1313

1414
To enable the context menu, configure the Grid's [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuItemModel.html) property. Use default items for built-in menu options or add custom items for tailored functionality. This feature improves interactivity by offering context-sensitive actions.
1515

1616
The context menu appears when right-clicking in these Grid areas:
17-
* **Header:** Right-click the header for column-specific actions.
18-
* **Content:** Right-click the main content for row-specific actions.
19-
* **Pager:** Right-click the pager for navigation options.
17+
* **Header:** Displays column-specific actions.
18+
* **Content:** Shows row-related options.
19+
* **Pager:** Offers navigation controls.
2020

2121
The default context menu items include:
2222

@@ -53,7 +53,6 @@ The default context menu items include:
5353
| `LastPage` | Navigate to the last page of the Grid. |
5454
| `NextPage` | Navigate to the next page of the Grid. |
5555

56-
**Example: Enable the context menu feature in the Grid**
5756

5857
{% tabs %}
5958
{% highlight razor tabtitle="Index.razor" %}
@@ -149,8 +148,6 @@ To configure custom context menu items:
149148
2. Specify properties such as **Text**, **Target**, and **Id** for each custom item.
150149
3. Handle actions using the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuClickEventArgs-1.html) event.
151150

152-
**Example: Add a custom context menu item to copy data with headers**
153-
154151
The [CopyAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_CopyAsync_System_Nullable_System_Boolean__) method is used to copy selected rows or cells, including headers, to the clipboard.
155152

156153
{% tabs %}
@@ -249,8 +246,6 @@ To configure **built-in** and **custom** context menu items:
249246
2. For custom items, specify properties such as **Text**, **Target**, and **Id**.
250247
3. Handle custom actions using the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuClickEventArgs-1.html) event.
251248

252-
**Example: Combine built-in and custom context menu items**
253-
254249
The [CopyAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_CopyAsync_System_Nullable_System_Boolean__) method copies selected rows or cells, including headers, to the clipboard.
255250

256251
{% tabs %}
@@ -342,16 +337,14 @@ public class OrderData
342337

343338
## Sub context menu items in DataGrid
344339

345-
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid supports hierarchical context menu structures, allowing sub-context menu items to appear as child options under a parent item. This feature helps organize multiple related actions under a single top-level context menu item.
340+
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid supports hierarchical context menus, allowing sub-items to be grouped under a parent menu item. This structure helps organize related actions and improves interface usability.
346341

347342
To configure sub-context menu items:
348343

349344
1. Define the [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuItemModel.html) property with a list of [ContextMenuItemModel](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuItemModel.html) objects.
350-
2. Add sub-items by specifying the collection for the Items property in ContextMenuItemModel.
345+
2. Add sub-items by specifying the collection for the Items property in `ContextMenuItemModel`.
351346
3. Handle actions using the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuClickEventArgs-1.html) event.
352347

353-
**Example: Sub Context Menu Items**
354-
355348
This example creates a sub-context menu titled **Clipboard**, which includes the sub-items **Copy** and **Copy With Header**. When the `ContextMenuItemClicked` event is triggered, the [CopyAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_CopyAsync_System_Nullable_System_Boolean__) method runs to copy data with or without headers.
356349

357350
{% tabs %}
@@ -461,19 +454,19 @@ public class OrderData
461454

462455
## Disable the context menu for specific columns in DataGrid
463456

464-
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows restricting the context menu on specific columns—particularly when those columns contain `sensitive` or `read-only` data.
457+
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows restricting the context menu for individual columns, which is useful when dealing with **sensitive** or **read-only** data.
465458

466-
This behavior can be controlled using the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event. This event is triggered before the context menu is displayed and provides access to the column details. By checking the column field, the menu can be conditionally cancelled.
459+
This behavior is controlled using the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event. The event is triggered before the context menu appears and provides access to column details. By checking the column's **Field** property, the context menu can be conditionally disabled.
467460

468461
To disable the context menu for a specific column:
469462

470463
1. Handle the [ContextMenuOpen](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridEvents-1.html#Syncfusion_Blazor_Grids_GridEvents_1_ContextMenuOpen) event of the Grid.
471-
2. Use the **Column.Field** property to identify the column.
472-
3. Set **args.Cancel** to `true` to prevent the menu from opening for that column.
473-
474-
**Example: Disable Context Menu for Specific Columns**
464+
2. Use the **args.Column.Field** property to identify the target column.
465+
3. Set **args.Cancel = true** to prevent the context menu from opening for that column.
475466

476-
The [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuClickEventArgs-1.html) event handles actions triggered by context menu item clicks. The [CopyAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_CopyAsync_System_Nullable_System_Boolean__) method executes for all columns except **Freight**, where the context menu is disabled.
467+
In this example:
468+
- The context menu is disabled for the **Freight** column.
469+
- The [CopyAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_CopyAsync_System_Nullable_System_Boolean__) method executes for all other columns when the **Copy with headers** option is selected.
477470

478471
{% tabs %}
479472
{% highlight razor tabtitle="Index.razor" %}
@@ -572,15 +565,13 @@ public class OrderData
572565

573566
## Enable or disable context menu items
574567

575-
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows dynamically enabling or disabling specific context menu items using the [Disabled](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Navigations.MenuItem.html#Syncfusion_Blazor_Navigations_MenuItem_Disabled) property. This feature is useful when certain actions, such as **Edit** or **Delete** should be restricted based on the column, row data, or custom logic.
568+
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows dynamic control over the availability of context menu items using the [Disabled](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Navigations.MenuItem.html#Syncfusion_Blazor_Navigations_MenuItem_Disabled) property. This feature is useful for conditionally restricting actions such as **Edit** or **Delete**, based on the column, row data, or custom logic.
576569

577570
To achieve this, handle the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event. The event is triggered before the context menu is displayed and allows enabling or disabling menu items dynamically based on conditions.
578571

579572
1. Handle the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event of the grid.
580-
2. Use the **Args.ContextMenuObj.Items** collection to access the menu items.
581-
3. Set the **Disabled** property of the required item(s) to `true` or `false` based on the defined logic.
582-
583-
**Example: Enable or Disable Context Menu Items Dynamically**
573+
2. Use the **args.ContextMenu.Items** collection to access the menu items.
574+
3. Set the **Disabled** property of the required item(s) to **true** or **false** based on the defined logic.
584575

585576
The **Copy** menu item is disabled for the **ShipCity** column and remains enabled for all other columns.
586577

@@ -611,7 +602,13 @@ The **Copy** menu item is disabled for the **ShipCity** column and remains enabl
611602

612603
private void OnContextMenuOpen(ContextMenuOpenEventArgs<OrderData> args)
613604
{
614-
args.ContextMenu.Items[0].Disabled = args.Column.Field == nameof(OrderData.ShipCity);
605+
foreach (var item in args.ContextMenu.Items)
606+
{
607+
if (item.Text == "Copy")
608+
{
609+
item.Disabled = args.Column.Field == nameof(OrderData.ShipCity);
610+
}
611+
}
615612
}
616613
}
617614

@@ -664,23 +661,21 @@ public class OrderData
664661
{% endhighlight %}
665662
{% endtabs %}
666663

667-
{% previewsample "https://blazorplayground.syncfusion.com/embed/BXreWNBkTWeKfahW?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
664+
{% previewsample "https://blazorplayground.syncfusion.com/embed/VXLoWMXeysxsYYwF?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
668665

669666
![Enable or Disable Context Menu Items](/images/context-menu/blazor-datagrid-enable-disable-context-menu-items.gif)
670667

671668
## Show or hide context menu
672669

673-
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows dynamically showing or hiding specific context menu items using the [Hidden](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Navigations.MenuItem.html#Syncfusion_Blazor_Navigations_MenuItem_Hidden) property. This feature is useful when certain actions, such as Edit or Delete, should be hidden based on the column, row data, or custom logic.
670+
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows dynamically showing or hiding specific context menu items using the [Hidden](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Navigations.MenuItem.html#Syncfusion_Blazor_Navigations_MenuItem_Hidden) property. This feature is useful for conditionally restricting actions such as **Edit** or **Delete**, based on the column, row data, or custom logic.
674671

675-
To achieve this, handle the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event. The event is triggered before the context menu is displayed and allows modifying the visibility of menu items dynamically based on conditions.
672+
To achieve this, handle the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event. The event is triggered before the context menu is displayed and allows dynamic modification of menu item visibility based on defined conditions.
676673

677674
To control the visibility of context menu items:
678675

679676
1. Handle the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/datagrid/events#contextmenuopen) event of the grid.
680-
2. Use the **Args.ContextMenuObj.Items** collection to access the menu items.
681-
3. Set the [Hidden](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Navigations.MenuItem.html#Syncfusion_Blazor_Navigations_MenuItem_Hidden) property of the required item(s) to `true` or `false` based on the defined logic.
682-
683-
**Example: Show or Hide Context Menu Items Dynamically**
677+
2. Use the **args.ContextMenu.Items** collection to access the menu items.
678+
3. Set the [Hidden](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Navigations.MenuItem.html#Syncfusion_Blazor_Navigations_MenuItem_Hidden) property of the required item(s) to **true** or **false** based on the defined logic.
684679

685680
The **Edit** menu item is hidden for the **CustomerID** column and remains visible for all other columns.
686681

@@ -711,7 +706,13 @@ The **Edit** menu item is hidden for the **CustomerID** column and remains visib
711706

712707
private void OnContextMenuOpen(ContextMenuOpenEventArgs<OrderData> args)
713708
{
714-
args.ContextMenu.Items[1].Hidden = args.Column.Field == nameof(OrderData.CustomerID);
709+
foreach (var item in args.ContextMenu.Items)
710+
{
711+
if (item.Text == "Edit Record")
712+
{
713+
item.Hidden = args.Column.Field == nameof(OrderData.CustomerID);
714+
}
715+
}
715716
}
716717
}
717718

@@ -780,10 +781,6 @@ To access row data on context menu click:
780781
2. Handle the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuClickEventArgs-1.html) event of the grid.
781782
3. Use the **RowInfo.RowData** property from the event arguments to access the full details of the selected row.
782783

783-
**Example: Fetch Row Details on Context Menu Click**
784-
785-
The **Fetch Data** menu item retrieves the details of the selected row and displays them below the grid.
786-
787784
{% tabs %}
788785
{% highlight razor tabtitle="Index.razor" %}
789786

0 commit comments

Comments
 (0)