-
Notifications
You must be signed in to change notification settings - Fork 80
Added new kb article grid-kb-conditionally-hide-command-buttons #2718
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 4 commits into
master
from
new-kb-grid-kb-conditionally-hide-command-buttons-cf7bcd9c885547e5be5bc421dec101cc
Jan 23, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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-conditionally-hide-command-buttons.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: Conditionally Hide Command Buttons in Blazor Grid | ||
| description: Learn how to conditionally show or hide command buttons in a Blazor Grid based on row data values. | ||
| type: how-to | ||
| page_title: How to Dynamically Hide Command Buttons in Blazor Grid | ||
| slug: grid-kb-conditionally-hide-command-buttons | ||
| tags: blazor, grid, commandbutton | ||
| res_type: kb | ||
| ticketid: 1675338 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td> | ||
| Grid for Blazor | ||
| </td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
| In some scenarios, you might want to conditionally show or hide command buttons in a [Grid for Blazor](slug://grid-overview) based on the data of the current row. For instance, you may want to display a delete button only for items that meet certain criteria. This article demonstrates how to achieve this behavior by using the context of the command column. | ||
|
|
||
| ## Solution | ||
| To conditionally show or hide command buttons in a Grid, use the context parameter of the `GridCommandColumn` to access the current row's data. Based on this data, you can conditionally render the command button. | ||
|
|
||
| ````RAZOR | ||
| @CustomCommandResult | ||
|
|
||
| <TelerikGrid Data=@GridData | ||
| EditMode="@GridEditMode.Inline" | ||
| OnUpdate="@HandleUpdate" | ||
| Pageable="true" | ||
| PageSize="15" | ||
| Height="500px"> | ||
| <GridColumns> | ||
| <GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="Employee ID" /> | ||
| <GridColumn Field=@nameof(SampleData.Name) Title="Employee Name" /> | ||
| <GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date" /> | ||
| <GridCommandColumn Context="dataItem"> | ||
| @{ | ||
| var item = (SampleData)dataItem; | ||
| } | ||
| <GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">Edit</GridCommandButton> | ||
| <GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true" OnClick="@HandleCustomSave">Save</GridCommandButton> | ||
| <GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton> | ||
| @if (item.ID % 2 == 0) | ||
| { | ||
| <GridCommandButton Command="MyOwnCommand" Icon="@SvgIcon.InfoCircle" ShowInEdit="false" OnClick="@HandleCustomButtonClick">My Command</GridCommandButton> | ||
| } | ||
| </GridCommandColumn> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private List<SampleData> GridData { get; set; } | ||
| private MarkupString CustomCommandResult { get; set; } | ||
|
|
||
| public class SampleData | ||
| { | ||
| public int ID { get; set; } | ||
| public string Name { get; set; } | ||
| public DateTime HireDate { get; set; } | ||
| } | ||
|
|
||
| private async Task HandleCustomSave(GridCommandEventArgs args) | ||
| { | ||
| SampleData theUpdatedItem = args.Item as SampleData; | ||
| } | ||
|
|
||
| private async Task HandleCustomButtonClick(GridCommandEventArgs args) | ||
| { | ||
| CustomCommandResult = new MarkupString(CustomCommandResult + string.Format("<br />Custom command triggered for item {0}", (args.Item as SampleData).ID)); | ||
| } | ||
|
|
||
| private async Task HandleUpdate(GridCommandEventArgs args) | ||
| { | ||
| SampleData theUpdatedItem = args.Item as SampleData; | ||
|
|
||
| await MyService.Update(theUpdatedItem); | ||
|
|
||
| await GetGridData(); | ||
| } | ||
|
|
||
| private async Task GetGridData() | ||
| { | ||
| GridData = await MyService.Read(); | ||
| } | ||
|
|
||
| protected override async Task OnInitializedAsync() | ||
| { | ||
| await GetGridData(); | ||
| } | ||
|
|
||
| public static class MyService | ||
| { | ||
| private static List<SampleData> _data { get; set; } = new List<SampleData>(); | ||
|
|
||
| public static async Task<List<SampleData>> Read() | ||
| { | ||
| if (_data.Count < 1) | ||
| { | ||
| _data = Enumerable.Range(1, 50).Select(i => new SampleData | ||
| { | ||
| ID = i, | ||
| Name = $"name {i}", | ||
| HireDate = DateTime.Now.AddDays(-i) | ||
| }).ToList(); | ||
| } | ||
|
|
||
| return await Task.FromResult(_data); | ||
| } | ||
|
|
||
| public static async Task Update(SampleData itemToUpdate) | ||
| { | ||
| var index = _data.FindIndex(i => i.ID == itemToUpdate.ID); | ||
| if (index != -1) | ||
| { | ||
| _data[index] = itemToUpdate; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ### Note | ||
| If you prefer not to remove the button from the DOM but simply hide it, you can conditionally set the `Class` parameter of the `GridCommandButton` tag and use CSS to hide the button. | ||
xristianstefanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ````RAZOR | ||
| <style> | ||
| .hide-command-button { | ||
| display: none; | ||
| } | ||
| </style> | ||
|
|
||
| @CustomCommandResult | ||
|
|
||
| <TelerikGrid Data=@GridData | ||
| EditMode="@GridEditMode.Inline" | ||
| OnUpdate="@HandleUpdate" | ||
| Pageable="true" | ||
| PageSize="15" | ||
| Height="500px"> | ||
| <GridColumns> | ||
| <GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="Employee ID" /> | ||
| <GridColumn Field=@nameof(SampleData.Name) Title="Employee Name" /> | ||
| <GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date" /> | ||
| <GridCommandColumn Context="dataItem"> | ||
| @{ | ||
| var item = (SampleData)dataItem; | ||
| } | ||
| <GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">Edit</GridCommandButton> | ||
| <GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true" OnClick="@HandleCustomSave">Save</GridCommandButton> | ||
| <GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton> | ||
| <GridCommandButton Command="MyOwnCommand" Class="@(item.ID % 2 != 0 ? "hide-command-button" : string.Empty)" Icon="@SvgIcon.InfoCircle" ShowInEdit="false" OnClick="@HandleCustomButtonClick">My Command</GridCommandButton> | ||
| </GridCommandColumn> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private List<SampleData> GridData { get; set; } | ||
| private MarkupString CustomCommandResult { get; set; } | ||
|
|
||
| public class SampleData | ||
| { | ||
| public int ID { get; set; } | ||
| public string Name { get; set; } | ||
| public DateTime HireDate { get; set; } | ||
| } | ||
|
|
||
| private async Task HandleCustomSave(GridCommandEventArgs args) | ||
| { | ||
| SampleData theUpdatedItem = args.Item as SampleData; | ||
| } | ||
|
|
||
| private async Task HandleCustomButtonClick(GridCommandEventArgs args) | ||
| { | ||
| CustomCommandResult = new MarkupString(CustomCommandResult + string.Format("<br />Custom command triggered for item {0}", (args.Item as SampleData).ID)); | ||
| } | ||
|
|
||
| private async Task HandleUpdate(GridCommandEventArgs args) | ||
| { | ||
| SampleData theUpdatedItem = args.Item as SampleData; | ||
|
|
||
| await MyService.Update(theUpdatedItem); | ||
|
|
||
| await GetGridData(); | ||
| } | ||
|
|
||
| private async Task GetGridData() | ||
| { | ||
| GridData = await MyService.Read(); | ||
| } | ||
|
|
||
| protected override async Task OnInitializedAsync() | ||
| { | ||
| await GetGridData(); | ||
| } | ||
|
|
||
| public static class MyService | ||
| { | ||
| private static List<SampleData> _data { get; set; } = new List<SampleData>(); | ||
|
|
||
| public static async Task<List<SampleData>> Read() | ||
| { | ||
| if (_data.Count < 1) | ||
| { | ||
| _data = Enumerable.Range(1, 50).Select(i => new SampleData | ||
| { | ||
| ID = i, | ||
| Name = $"name {i}", | ||
| HireDate = DateTime.Now.AddDays(-i) | ||
| }).ToList(); | ||
| } | ||
|
|
||
| return await Task.FromResult(_data); | ||
| } | ||
|
|
||
| public static async Task Update(SampleData itemToUpdate) | ||
| { | ||
| var index = _data.FindIndex(i => i.ID == itemToUpdate.ID); | ||
| if (index != -1) | ||
| { | ||
| _data[index] = itemToUpdate; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
| * [Grid Overview](slug://grid-overview) | ||
| * [Grid Command Column](slug://components/grid/columns/command) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified or even removed. The focus of the KB is different and doesn't require MarkupString or custom commands. You can use a built-in Delete command.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let that be the difference from the Incell editing demo?