Skip to content

Commit 95437a6

Browse files
documentation(984305): Updated blazor topic.
1 parent 22571a1 commit 95437a6

File tree

4 files changed

+482
-208
lines changed

4 files changed

+482
-208
lines changed

blazor/gantt-chart/context-menu.md

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ documentation: ug
99

1010
# Context Menu in Blazor Gantt Chart Component
1111

12-
The [Blazor Gantt Chart](https://www.syncfusion.com/blazor-components/blazor-gantt-chart) component allows you to perform quick actions by using the context menu. When right-clicking the context menu, the context menu options will be shown. To enable this feature, set the [EnableContextMenu](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_EnableContextMenu) to “true”. The default context menu options are enabled using the [GanttEditSettings](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEditSettings.html) property. The context menu options can be customized using the [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems) property.
12+
The Blazor Gantt Chart component provides quick access to actions through a context menu. On right-click, context menu options are displayed based on the clicked element.
13+
14+
To enable this feature, set the [EnableContextMenu](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_EnableContextMenu) to **true**. The context menu options can be customized using the [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems) property.
1315

1416
The default items are listed in the following table:
1517

@@ -33,6 +35,7 @@ Items| Description
3335
{% highlight razor tabtitle="Index.razor" %}
3436

3537
@using Syncfusion.Blazor.Gantt
38+
3639
<SfGantt DataSource="@TaskCollection" Height="450px" EnableContextMenu="true" AllowSorting="true" AllowResizing="true" Width="900px" HighlightWeekends="true">
3740
<GanttTaskFields Id="TaskID" Name="TaskName" StartDate="StartDate" EndDate="EndDate" Duration="Duration" Progress="Progress"
3841
Dependency="Predecessor" ParentID="ParentID"></GanttTaskFields>
@@ -41,10 +44,12 @@ Items| Description
4144

4245
@code {
4346
private List<TaskData> TaskCollection { get; set; }
47+
4448
protected override void OnInitialized()
4549
{
4650
this.TaskCollection = GetTaskCollection();
4751
}
52+
4853
public class TaskData
4954
{
5055
public int TaskID { get; set; }
@@ -80,37 +85,39 @@ Items| Description
8085

8186
## Custom context menu items
8287

83-
The custom context menu items can be added by defining the [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems) as a collection of [ContextMenuItemModel](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ContextMenuItemModel.html). Actions for these customized items can be defined in the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuItemClicked) event.
84-
85-
The following sample code demonstrates defining a custom context menu item and its corresponding action in the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuItemClicked) event.
88+
You can configure custom context menu items by assigning a collection of `ContextMenuItemModel` to the [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems) property. To define actions for these items, use the [ContextMenuItemClicked](https://blazor.syncfusion.com/documentation/gantt-chart/events#contextmenuitemclicked) event.
8689

8790
{% tabs %}
8891
{% highlight razor tabtitle="Index.razor" %}
8992

9093
@using Syncfusion.Blazor.Gantt
9194
@using Syncfusion.Blazor.Grids
92-
<SfGantt @ref="Gantt" DataSource="@TaskCollection" Height="450px" Width="700px" ContextMenuItems="@contextMenuItems">
95+
96+
<SfGantt @ref="GanttInstance" DataSource="@TaskCollection" Height="450px" Width="700px" ContextMenuItems="@contextMenuItems">
9397
<GanttTaskFields Id="TaskID" Name="TaskName" StartDate="StartDate" EndDate="EndDate" Duration="Duration" Progress="Progress" ParentID="ParentID">
9498
</GanttTaskFields>
9599
<GanttEvents ContextMenuItemClicked=ContextMenuItemClickedHandler TValue="TaskData"></GanttEvents>
96100
</SfGantt>
97101

98102
@code {
99103
public List<TaskData> TaskCollection { get; set; }
100-
private SfGantt<TaskData> Gantt;
104+
private SfGantt<TaskData> GanttInstance;
105+
101106
private List<ContextMenuItemModel> contextMenuItems = new List<ContextMenuItemModel>()
102107
{
103108
new ContextMenuItemModel(){Text="Refresh", Target=".e-content",Id="Refresh"}
104109
};
110+
105111
protected override void OnInitialized()
106112
{
107113
this.TaskCollection = GetTaskCollection();
108114
}
115+
109116
private async void ContextMenuItemClickedHandler(ContextMenuClickEventArgs<TaskData> args)
110117
{
111118
if(args.Item.Id == "Refresh")
112119
{
113-
await Gantt.RefreshAsync();
120+
await GanttInstance.RefreshAsync();
114121
}
115122
}
116123

@@ -148,36 +155,38 @@ The following sample code demonstrates defining a custom context menu item and i
148155

149156
## Built-in and custom context menu items
150157

151-
Gantt Chart has an option to use both built-in and custom context menu items at the same time.
152-
153-
The following sample code demonstrates defining built-in and custom context menu items and custom context menu item corresponding action in the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuItemClicked) event,
158+
You can configure built-in and custom context menu items at the same time in the Gantt Chart by assigning a collection of `ContextMenuItemModel` to the `[ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems) property. The corresponding actions for custom items are handled through the [ContextMenuItemClicked](https://blazor.syncfusion.com/documentation/gantt-chart/events#contextmenuitemclicked) event.
154159

155160
{% tabs %}
156161
{% highlight razor tabtitle="Index.razor" %}
157162

158163
@using Syncfusion.Blazor.Gantt
159164
@using Syncfusion.Blazor.Grids
160-
<SfGantt @ref="Gantt" DataSource="@TaskCollection" Height="450px" Width="700px"
161-
ContextMenuItems="@(new List<Object>() { "Add", new ContextMenuItemModel { Text = "Refresh", Target = ".e-content", Id = "Refresh" } })">
165+
166+
<SfGantt @ref="GanttInstance" DataSource="@TaskCollection" Height="450px" Width="700px"
167+
ContextMenuItems="@(new List<Object>() { "Add", new ContextMenuItemModel { Text = "Copy with headers", Target = ".e-content", Id = "copywithheader" } })">
162168
<GanttTaskFields Id="TaskID" Name="TaskName" StartDate="StartDate" EndDate="EndDate"
163169
Duration="Duration" Progress="Progress" ParentID="ParentID">
164170
</GanttTaskFields>
165171
<GanttEditSettings AllowAdding="true"></GanttEditSettings>
166172
<GanttEvents ContextMenuItemClicked=ContextMenuItemClickedHandler TValue="TaskData">
167173
</GanttEvents>
168174
</SfGantt>
175+
169176
@code {
170177
public List<TaskData> TaskCollection { get; set; }
171-
private SfGantt<TaskData> Gantt;
178+
private SfGantt<TaskData> GanttInstance;
179+
172180
protected override void OnInitialized()
173181
{
174182
this.TaskCollection = GetTaskCollection();
175183
}
184+
176185
private async void ContextMenuItemClickedHandler(ContextMenuClickEventArgs<TaskData> args)
177186
{
178-
if (args.Item.Id == "Refresh")
187+
if (args.Item.Id == "copywithheader")
179188
{
180-
await Gantt.RefreshAsync();
189+
await GanttInstance.CopyAsync(true);
181190
}
182191
}
183192
public class TaskData
@@ -209,13 +218,17 @@ The following sample code demonstrates defining built-in and custom context menu
209218
{% endhighlight %}
210219
{% endtabs %}
211220

212-
{% previewsample "https://blazorplayground.syncfusion.com/embed/VZrSiZDEUogRNlUf?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
221+
{% previewsample "https://blazorplayground.syncfusion.com/embed/LNLoWNWHhsckRdfz?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
213222

214223
## Sub context menu items
215224

216-
The sub-context menu items can be added by defining the collection of **MenuItems** for **Items** Property in [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems). Actions for these customized items can be defined in the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuItemClicked) event.
225+
To configure nested context menu items (sub-menus) in the Blazor Gantt Chart, follow these steps:
217226

218-
The following sample code demonstrates defining the sub-context menu item and its corresponding action in the [ContextMenuItemClicked](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuItemClicked) event,
227+
1. Define a list of `ContextMenuItemModel` objects using the [ContextMenuItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.SfGantt-1.html#Syncfusion_Blazor_Gantt_SfGantt_1_ContextMenuItems) property.
228+
2. Add sub-items by assigning a collection of `MenuItems` to the `Items` property within each `ContextMenuItemModel`.
229+
3. Use the [ContextMenuItemClicked](https://blazor.syncfusion.com/documentation/gantt-chart/events#contextmenuitemclicked) event to handle actions for individual menu items.
230+
231+
The following example demonstrates how to configure a sub-context menu titled **Gantt Action**, which includes the sub-items **Copy with headers** and **Edit**.
219232

220233
{% tabs %}
221234
{% highlight razor tabtitle="Index.razor" %}
@@ -236,19 +249,25 @@ The following sample code demonstrates defining the sub-context menu item and it
236249
private SfGantt<TaskData> Gantt;
237250
private List<ContextMenuItemModel> contextMenuItems = new List<ContextMenuItemModel>()
238251
{
239-
new ContextMenuItemModel{Text="Gantt Action",Target=".e-content",Id="GanttAction",
240-
Items=new List<MenuItem>(){new MenuItem {Text="Refresh",Id= "Refresh"},new MenuItem {Text="Edit",Id= "Edit"} } }
252+
new ContextMenuItemModel{
253+
Text="Gantt Action",Target=".e-content",Id="GanttAction",
254+
Items=new List<MenuItem>(){
255+
new MenuItem {Text="Copy with headers",Id= "copywithheader"},
256+
new MenuItem {Text="Edit",Id= "Edit"}
257+
}
258+
}
241259
};
242260

243261
protected override void OnInitialized()
244262
{
245263
this.TaskCollection = GetTaskCollection();
246264
}
265+
247266
public async void ContextMenuItemClickedHandler(ContextMenuClickEventArgs<TaskData> args)
248267
{
249-
if (args.Item.Id == "Refresh")
268+
if (args.Item.Id == "copywithheader")
250269
{
251-
await Gantt.RefreshAsync();
270+
await Gantt.CopyAsync(true);
252271
}
253272
if (args.Item.Id == "Edit")
254273
{
@@ -286,13 +305,13 @@ The following sample code demonstrates defining the sub-context menu item and it
286305
{% endhighlight %}
287306
{% endtabs %}
288307

289-
{% previewsample "https://blazorplayground.syncfusion.com/embed/hthyDaCuziCqRiFF?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
308+
{% previewsample "https://blazorplayground.syncfusion.com/embed/rZLSMDsnBVFIKLRF?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
290309

291310
## Disable the context menu for specific columns
292311

293-
Context Menu can be prevented for specific columns using [ContextMenuOpen](https://blazor.syncfusion.com/documentation/gantt-chart/events#contextmenuopen) event of Gantt. This event will be triggered before opening the ContextMenu. You can prevent the context menu from opening by defining the **Cancel** arguments of ContextMenuOpen to **false**.
312+
To disable the context menu for specific columns in the Gantt Chart, use the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/gantt-chart/events#contextmenuopen) event. This event is triggered before the context menu is displayed, and setting the `Cancel` argument to **false** will disable the menu for the targeted columns.
294313

295-
The following sample code demonstrates how to disable the context for specific column using event arguments of ContextMenuOpen event,
314+
The following sample code demonstrates how to disable the context menu for the **Duration** column.
296315

297316
{% tabs %}
298317
{% highlight razor tabtitle="Index.razor" %}
@@ -314,17 +333,20 @@ The following sample code demonstrates how to disable the context for specific c
314333
{
315334
this.TaskCollection = GetTaskCollection();
316335
}
336+
317337
private List<ContextMenuItemModel> contextMenuItems = new List<ContextMenuItemModel>()
318338
{
319339
new ContextMenuItemModel(){Text="Refresh", Target=".e-content",Id="refresh"}
320340
};
341+
321342
public void OnContextMenuOpen(ContextMenuOpenEventArgs<TaskData> Args)
322343
{
323344
if (Args.Column != null && Args.Column.Field == "Duration")
324345
{
325-
Args.Cancel = true; // To prevent the context menu from opening
346+
Args.Cancel = true; // To prevent the context menu from opening.
326347
}
327348
}
349+
328350
private async void ContextMenuItemClickedHandler(ContextMenuClickEventArgs<TaskData> args)
329351
{
330352
if (args.Item.Id == "refresh")
@@ -365,11 +387,12 @@ The following sample code demonstrates how to disable the context for specific c
365387

366388
{% previewsample "https://blazorplayground.syncfusion.com/embed/BXVeXaiOJCAcXLUo?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %}
367389

390+
368391
## Disable context menu items dynamically
369392

370-
You can enable or disable the context menu items using the **Disabled** property. Here, you can enable and disable the **Edit** context menu items in [ContextMenuOpen](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuOpen) event of Gantt. This event will be triggered before opening the ContextMenu. You can disable the context menu item by defining the corresponding context menu items **Disabled** property as **true**.
393+
To dynamically disable specific context menu items based on conditions, set the `Disabled` property to **true** within the [ContextMenuOpen](https://blazor.syncfusion.com/documentation/gantt-chart/events#contextmenuopen) event of the Gantt Chart.
371394

372-
The following sample code demonstrates how to enable or disable context menu items dynamically in Gantt using event arguments of [ContextMenuOpen](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Gantt.GanttEvents-1.html#Syncfusion_Blazor_Gantt_GanttEvents_1_ContextMenuOpen) event,
395+
The following sample code demonstrates how to disable the context menu items for the **Duration** column, while keeping it enabled for the remaining columns.
373396

374397
{% tabs %}
375398
{% highlight razor tabtitle="Index.razor" %}
@@ -391,13 +414,18 @@ The following sample code demonstrates how to enable or disable context menu ite
391414
private List<ContextMenuItemModel> contextMenuItems = new List<ContextMenuItemModel>()
392415
{
393416
new ContextMenuItemModel{Text="Gantt Action",Target=".e-content",Id="GanttAction",
394-
Items=new List<MenuItem>(){new MenuItem{Text="Refresh",Id="Refresh"},new MenuItem{Text="Edit",Id= "Edit"}}}
417+
Items=new List<MenuItem>(){
418+
new MenuItem{Text="Refresh",Id="Refresh"},
419+
new MenuItem{Text="Edit",Id= "Edit"}
420+
}
421+
}
395422
};
396423

397424
protected override void OnInitialized()
398425
{
399426
this.TaskCollection = GetTaskCollection();
400427
}
428+
401429
public async void ContextMenuItemClickedHandler(ContextMenuClickEventArgs<TaskData> args)
402430
{
403431
if (args.Item.Id == "Refresh")
@@ -409,18 +437,17 @@ The following sample code demonstrates how to enable or disable context menu ite
409437
await Gantt.OpenEditDialogAsync();
410438
}
411439
}
440+
412441
public void OnContextMenuOpen(ContextMenuOpenEventArgs<TaskData> Args)
413442
{
414-
#pragma warning disable BL0005
415-
if (Args.Column != null && Args.Column.Field == "Duration") // You can check condition based on your requirement
443+
if (Args.Column != null && Args.Column.Field == "Duration")
416444
{
417-
Args.ContextMenu.Items[0].Disabled = true; // To disable edit context menu item
445+
Args.ContextMenu.Items[0].Disabled = true; // To disable edit context menu item.
418446
}
419447
else
420448
{
421-
Args.ContextMenu.Items[0].Disabled = false; // To enable edit context menu item
449+
Args.ContextMenu.Items[0].Disabled = false; // To enable edit context menu item.
422450
}
423-
#pragma warning restore BL0005
424451
}
425452

426453
public class TaskData

0 commit comments

Comments
 (0)