Skip to content

Commit de2d2e3

Browse files
author
KB Bot
committed
Added new kb article grid-kb-conditionally-hide-command-buttons
1 parent 47d5b17 commit de2d2e3

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
title: Conditionally Hiding Command Buttons in Grid for Blazor
3+
description: Learn how to conditionally show or hide command buttons in a Blazor Grid based on row data values.
4+
type: how-to
5+
page_title: Dynamically Hiding Command Buttons in Blazor Grid Based on Row Values
6+
slug: grid-kb-conditionally-hide-command-buttons
7+
tags: grid,blazor,commandbutton,conditional,visibility,row
8+
res_type: kb
9+
ticketid: 1675338
10+
---
11+
12+
## Description
13+
In some scenarios, you might want to conditionally show or hide command buttons in a [Grid for Blazor](https://docs.telerik.com/blazor-ui/components/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.
14+
15+
This knowledge base article also answers the following questions:
16+
- How can I hide a GridCommandButton based on a row value in Blazor?
17+
- What is the way to conditionally display command buttons in a Telerik Blazor Grid?
18+
- Can I dynamically control the visibility of command buttons in a Grid for Blazor?
19+
20+
## Solution
21+
To conditionally show or hide command buttons in a Grid for Blazor, 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.
22+
23+
````RAZOR
24+
@CustomCommandResult
25+
26+
<TelerikGrid Data=@GridData EditMode="@GridEditMode.Inline" OnUpdate="@MyOnUpdateHandler"
27+
Pageable="true" PageSize="15" Height="500px">
28+
<GridColumns>
29+
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="Employee ID" />
30+
<GridColumn Field=@nameof(SampleData.Name) Title="Employee Name" />
31+
<GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date" />
32+
<GridCommandColumn Context="dataItem">
33+
@{
34+
var item = (SampleData)dataItem;
35+
}
36+
<GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">Edit</GridCommandButton>
37+
<GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true" OnClick="@CustomSaveOnClickHandler">Save</GridCommandButton>
38+
<GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton>
39+
@if (item.ID % 2 == 0)
40+
{
41+
<GridCommandButton Command="MyOwnCommand" Icon="@SvgIcon.InfoCircle" ShowInEdit="false" OnClick="@MyCustomCommandOnClickHandler">My Command</GridCommandButton>
42+
}
43+
</GridCommandColumn>
44+
</GridColumns>
45+
</TelerikGrid>
46+
47+
@code {
48+
private List<SampleData> GridData { get; set; }
49+
private MarkupString CustomCommandResult;
50+
51+
public class SampleData
52+
{
53+
public int ID { get; set; }
54+
public string Name { get; set; }
55+
public DateTime HireDate { get; set; }
56+
}
57+
58+
private async Task CustomSaveOnClickHandler(GridCommandEventArgs args)
59+
{
60+
SampleData theUpdatedItem = args.Item as SampleData;
61+
}
62+
63+
private async Task MyCustomCommandOnClickHandler(GridCommandEventArgs args)
64+
{
65+
CustomCommandResult = new MarkupString(CustomCommandResult + string.Format("<br />Custom command triggered for item {0}", (args.Item as SampleData).ID));
66+
}
67+
68+
private async Task MyOnUpdateHandler(GridCommandEventArgs args)
69+
{
70+
SampleData theUpdatedItem = args.Item as SampleData;
71+
72+
await MyService.Update(theUpdatedItem);
73+
74+
await GetGridData();
75+
}
76+
77+
private async Task GetGridData()
78+
{
79+
GridData = await MyService.Read();
80+
}
81+
82+
protected override async Task OnInitializedAsync()
83+
{
84+
await GetGridData();
85+
}
86+
87+
public static class MyService
88+
{
89+
private static List<SampleData> _data { get; set; } = new List<SampleData>();
90+
91+
public static async Task<List<SampleData>> Read()
92+
{
93+
if (_data.Count < 1)
94+
{
95+
for (int i = 1; i < 50; i++)
96+
{
97+
_data.Add(new SampleData()
98+
{
99+
ID = i,
100+
Name = "name " + i,
101+
HireDate = DateTime.Now.AddDays(-i)
102+
});
103+
}
104+
}
105+
106+
return await Task.FromResult(_data);
107+
}
108+
109+
public static async Task Update(SampleData itemToUpdate)
110+
{
111+
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
112+
if (index != -1)
113+
{
114+
_data[index] = itemToUpdate;
115+
}
116+
}
117+
}
118+
}
119+
````
120+
121+
### Note
122+
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 utilize CSS to hide the button.
123+
124+
## See Also
125+
- [Blazor Grid Overview](https://docs.telerik.com/blazor-ui/components/grid/overview)
126+
- [Blazor Grid Command Column](https://docs.telerik.com/blazor-ui/components/grid/columns/command)

0 commit comments

Comments
 (0)