|
| 1 | +--- |
| 2 | +title: Displaying Loader During Actions in TelerikGrid with ContextMenu |
| 3 | +description: Learn how to show a loading indicator while performing actions from a ContextMenu in a TelerikGrid for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Display a Loader in TelerikGrid for Blazor During ContextMenu Actions |
| 6 | +slug: grid-kb-loader-during-contextmenu-actions |
| 7 | +tags: contextmenu,grid,loader,loading, loadercontainer,actions |
| 8 | +res_type: kb |
| 9 | +ticketid: 1675767 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + <td>Context Menu for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +When using the TelerikGrid with ContextMenu for various row actions, there is a noticeable delay between selecting an action and its execution. This delay could be due to data retrieval or data manipulation. The goal is to display a loading notice to the user while the data loads and the result is not yet visible. |
| 27 | + |
| 28 | +This knowledge base article also answers the following questions: |
| 29 | +- How to use the TelerikLoaderContainer during data fetch operations in Blazor? |
| 30 | +- How to show a loading indicator while waiting for an action? |
| 31 | +- How to improve user experience during delayed operations in TelerikGrid with ContextMenu in Blazor? |
| 32 | + |
| 33 | +## Solution |
| 34 | + |
| 35 | +To display a spinner during delayed operations initiated from a ContextMenu in a TelerikGrid, utilize the [TelerikLoaderContainer](https://www.telerik.com/blazor-ui/documentation/components/loadercontainer/overview) component. Display the TelerikLoaderContainer while the data is being loaded or an action is being performed, and hide it once the operation is complete. |
| 36 | + |
| 37 | +````RAZOR |
| 38 | +@using System.Collections.Generic |
| 39 | +@using System.Collections.ObjectModel |
| 40 | +@using Telerik.Blazor.Components |
| 41 | +
|
| 42 | +<TelerikContextMenu @ref="@ContextMenuRef" Data="@MenuItems" |
| 43 | +OnClick="@((MenuItem item) => ContextMenuClickHandler(item))"> |
| 44 | +</TelerikContextMenu> |
| 45 | +
|
| 46 | +<TelerikLoaderContainer Visible="@LoaderVisible" Text="Loading, please wait..."></TelerikLoaderContainer> |
| 47 | +
|
| 48 | +<TelerikGrid Data="@GridData" @ref="@GridRef" |
| 49 | +EditMode="@GridEditMode.Inline" |
| 50 | +Height="500px" |
| 51 | +Pageable="true" |
| 52 | +OnCreate="@CreateItem" OnUpdate="@UpdateHandler" |
| 53 | +OnRowContextMenu="@OnContextMenu" |
| 54 | +SelectionMode="@GridSelectionMode.Multiple" |
| 55 | +@bind-SelectedItems="@SelectedItems"> |
| 56 | + <GridToolBarTemplate> |
| 57 | + <GridCommandButton Command="Add" Icon="@SvgIcon.Plus">Add Employee</GridCommandButton> |
| 58 | + </GridToolBarTemplate> |
| 59 | + <GridColumns> |
| 60 | + <GridColumn Field=@nameof(SampleData.ID) Editable="false" /> |
| 61 | + <GridColumn Field=@nameof(SampleData.Name) /> |
| 62 | + <GridCommandColumn> |
| 63 | + <GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true">Save</GridCommandButton> |
| 64 | + <GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton> |
| 65 | + </GridCommandColumn> |
| 66 | + </GridColumns> |
| 67 | +</TelerikGrid> |
| 68 | +
|
| 69 | +@code { |
| 70 | + private ObservableCollection<SampleData> GridData { get; set; } |
| 71 | + private List<MenuItem> MenuItems { get; set; } |
| 72 | + private IEnumerable<SampleData> SelectedItems { get; set; } = Enumerable.Empty<SampleData>(); |
| 73 | + private SampleData SelectedPerson { get; set; } |
| 74 | + private TelerikContextMenu<MenuItem> ContextMenuRef { get; set; } |
| 75 | + private TelerikGrid<SampleData> GridRef { get; set; } |
| 76 | + private bool LoaderVisible { get; set; } |
| 77 | +
|
| 78 | + private async Task ContextMenuClickHandler(MenuItem item) |
| 79 | + { |
| 80 | + LoaderVisible = true; |
| 81 | + // Perform the action, such as data loading or another operation. |
| 82 | + await Task.Delay(3000); // Simulate a delay for demonstration. |
| 83 | + LoaderVisible = false; |
| 84 | + // Proceed with action-specific logic after the delay. |
| 85 | + } |
| 86 | +
|
| 87 | + // Additional component logic such as data initialization and CRUD operations. |
| 88 | +} |
| 89 | +```` |
| 90 | + |
| 91 | +## See Also |
| 92 | + |
| 93 | +- [TelerikLoaderContainer Overview](https://www.telerik.com/blazor-ui/documentation/components/loadercontainer/overview) |
| 94 | +- [TelerikGrid Documentation](https://docs.telerik.com/blazor-ui/components/grid/overview) |
| 95 | +- [TelerikContextMenu Documentation](https://docs.telerik.com/blazor-ui/components/contextmenu/overview) |
0 commit comments