-
Notifications
You must be signed in to change notification settings - Fork 81
Added new kb article grid-kb-loader-during-contextmenu-actions #2717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dimodi
merged 5 commits into
master
from
new-kb-grid-kb-loader-during-contextmenu-actions-465c440840c94328913eeb780e9e6d3a
Jan 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5edee82
Added new kb article grid-kb-loader-during-contextmenu-actions
5a6c5f3
chore(Grid): address comments
xristianstefanov 09f5bbe
chore(Grid): address latest comments
xristianstefanov cd50ddf
chore(Grid): return delay
xristianstefanov 08910ce
chore(Grid): remove delete action as per last comment
xristianstefanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
237 changes: 237 additions & 0 deletions
237
knowledge-base/grid-loader-during-contextmenu-actions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| --- | ||
| title: Display Loader During Actions in TelerikGrid with ContextMenu | ||
| description: Learn how to show a loading indicator while performing actions from a ContextMenu in a TelerikGrid for Blazor. | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type: how-to | ||
| page_title: How to Display a Loader in TelerikGrid for Blazor During ContextMenu Actions | ||
| slug: grid-kb-loader-during-contextmenu-actions | ||
| tags: contextmenu,grid,loader,loading, loadercontainer,actions | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| res_type: kb | ||
| ticketid: 1675767 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Grid for Blazor</td> | ||
| <td>Context Menu for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| When the TelerikGrid is used 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. | ||
|
|
||
| This knowledge base article also answers the following questions: | ||
| - How to use the TelerikLoaderContainer during data fetch operations in Blazor? | ||
| - How to show a loading indicator while waiting for an action? | ||
| - How to improve user experience during delayed operations in TelerikGrid with ContextMenu in Blazor? | ||
|
|
||
| ## Solution | ||
|
|
||
| To display a spinner during delayed operations initiated from a ContextMenu in a TelerikGrid, utilize the [TelerikLoaderContainer](slug://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. | ||
|
|
||
| ````RAZOR | ||
| @using System.Collections.ObjectModel | ||
|
|
||
| <TelerikContextMenu @ref="@ContextMenuRef" Data="@MenuItems" | ||
| OnClick="@((MenuItem item) => ContextMenuClickHandler(item))"> | ||
| </TelerikContextMenu> | ||
|
|
||
| <TelerikLoaderContainer Visible="@LoaderVisible" Text="Please wait..." /> | ||
|
|
||
| <TelerikGrid Data="@GridData" @ref="@GridRef" | ||
| EditMode="@GridEditMode.Inline" | ||
| Height="500px" | ||
| Pageable="true" | ||
| OnCreate="@CreateItem" OnUpdate="@UpdateHandler" | ||
| OnRowContextMenu="@OnContextMenu" | ||
| SelectionMode="@GridSelectionMode.Multiple" | ||
| @bind-SelectedItems="@SelectedItems"> | ||
| <GridToolBarTemplate> | ||
| <GridCommandButton Command="Add" Icon="@SvgIcon.Plus">Add Employee</GridCommandButton> | ||
| </GridToolBarTemplate> | ||
| <GridColumns> | ||
| <GridColumn Field=@nameof(SampleData.ID) Editable="false" /> | ||
| <GridColumn Field=@nameof(SampleData.Name) /> | ||
| <GridCommandColumn> | ||
| <GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true">Save</GridCommandButton> | ||
| <GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton> | ||
| </GridCommandColumn> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @if (SelectedItems.Any()) | ||
| { | ||
| <ul> | ||
| @foreach (var item in SelectedItems) | ||
| { | ||
| <li>@item.Name</li> | ||
| } | ||
| </ul> | ||
| } | ||
|
|
||
| @code { | ||
| private ObservableCollection<SampleData> GridData { get; set; } | ||
| private List<MenuItem> MenuItems { get; set; } | ||
| private IEnumerable<SampleData> SelectedItems { get; set; } = Enumerable.Empty<SampleData>(); | ||
| private SampleData SelectedPerson { get; set; } | ||
| private TelerikContextMenu<MenuItem> ContextMenuRef { get; set; } | ||
| private TelerikGrid<SampleData> GridRef { get; set; } | ||
| private bool LoaderVisible { get; set; } | ||
|
|
||
| public class MenuItem | ||
| { | ||
| public string Text { get; set; } | ||
| public ISvgIcon Icon { get; set; } | ||
| public Action Action { get; set; } | ||
| public string CommandName { get; set; } | ||
| } | ||
|
|
||
| private async Task OnContextMenu(GridRowClickEventArgs args) | ||
| { | ||
| var argsItem = args.Item as SampleData; | ||
|
|
||
| SelectedPerson = argsItem; | ||
|
|
||
| if (args.EventArgs is MouseEventArgs mouseEventArgs) | ||
| { | ||
| await ContextMenuRef.ShowAsync(mouseEventArgs.ClientX, mouseEventArgs.ClientY); | ||
| } | ||
| } | ||
|
|
||
| private async Task ContextMenuClickHandler(MenuItem item) | ||
| { | ||
| if (item.Action != null) | ||
| { | ||
| item.Action.Invoke(); | ||
| } | ||
| else | ||
| { | ||
| switch (item.CommandName) | ||
| { | ||
| case "BeginEdit": | ||
| LoaderVisible = true; | ||
| await Task.Delay(3000); //initiates delay | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LoaderVisible = false; | ||
|
|
||
| var currState = GridRef.GetState(); | ||
| currState.InsertedItem = null; | ||
| SampleData itemToEdit = SampleData.GetClonedInstance(GridData.Where(itm => itm.ID == SelectedPerson.ID).FirstOrDefault()); | ||
| currState.OriginalEditItem = itemToEdit; | ||
| await GridRef.SetStateAsync(currState); | ||
| break; | ||
| case "ToggleSelect": | ||
| LoaderVisible = true; | ||
| await Task.Delay(3000); //initiates delay | ||
| LoaderVisible = false; | ||
|
|
||
| var selItems = SelectedItems.ToList(); | ||
| if (SelectedItems.Contains(SelectedPerson)) | ||
| { | ||
| selItems.Remove(SelectedPerson); | ||
| } | ||
| else | ||
| { | ||
| selItems.Add(SelectedPerson); | ||
| } | ||
| SelectedItems = selItems; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| SelectedPerson = null; // clean up | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| MenuItems = new List<MenuItem>() | ||
| { | ||
| new MenuItem(){ Text = "Select", Icon = SvgIcon.CheckboxChecked, CommandName="ToggleSelect" }, | ||
| new MenuItem(){ Text = "Edit", Icon = SvgIcon.Pencil, CommandName="BeginEdit" }, | ||
| new MenuItem(){ Text = "Delete", Icon = SvgIcon.Trash, Action = DeleteItem } | ||
| }; | ||
|
|
||
| GridData = new ObservableCollection<SampleData>(); | ||
| var rand = new Random(); | ||
|
|
||
| for (int i = 0; i < 100; i++) | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| GridData.Add(new SampleData() | ||
| { | ||
| ID = i, | ||
| Name = "Employee " + i.ToString(), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private async Task CreateItem(GridCommandEventArgs args) | ||
| { | ||
| var argsItem = args.Item as SampleData; | ||
|
|
||
| argsItem.ID = GridData.Count + 1; | ||
|
|
||
| GridData.Insert(0, argsItem); | ||
| } | ||
|
|
||
| private void DeleteItem() | ||
| { | ||
| var argsItem = SelectedPerson; | ||
|
|
||
| GridData.Remove(argsItem); | ||
| } | ||
|
|
||
| private async Task UpdateHandler(GridCommandEventArgs args) | ||
| { | ||
| var argsItem = args.Item as SampleData; | ||
|
|
||
| var index = GridData.ToList().FindIndex(i => i.ID == argsItem.ID); | ||
| if (index != -1) | ||
| { | ||
| GridData[index] = argsItem; | ||
| } | ||
| } | ||
|
|
||
| public class SampleData | ||
| { | ||
| public int ID { get; set; } | ||
| public string Name { get; set; } | ||
|
|
||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| if (obj is SampleData) | ||
| { | ||
| return this.ID == (obj as SampleData).ID; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public SampleData() | ||
| { | ||
|
|
||
| } | ||
|
|
||
| public SampleData(SampleData itmToClone) | ||
| { | ||
| this.ID = itmToClone.ID; | ||
| this.Name = itmToClone.Name; | ||
| } | ||
|
|
||
| public static SampleData GetClonedInstance(SampleData itmToClone) | ||
| { | ||
| return new SampleData(itmToClone); | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [LoaderContainer Overview](slug://loadercontainer-overview) | ||
| - [Grid Documentation](slug://grid-overview) | ||
| - [ContextMenu Documentation](slug://contextmenu-overview) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.