Skip to content

Commit 5f7018d

Browse files
chore(Grid): address latest comments
1 parent cb7827c commit 5f7018d

File tree

1 file changed

+113
-14
lines changed

1 file changed

+113
-14
lines changed

knowledge-base/grid-conditionally-hide-command-buttons.md

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to conditionally show or hide command buttons in a Blazor
44
type: how-to
55
page_title: How to Dynamically Hide Command Buttons in Blazor Grid
66
slug: grid-kb-conditionally-hide-command-buttons
7-
tags: grid,blazor,commandbutton,conditional,visibility,row
7+
tags: blazor, grid, commandbutton
88
res_type: kb
99
ticketid: 1675338
1010
---
@@ -31,11 +31,11 @@ To conditionally show or hide command buttons in a Grid, use the context paramet
3131
````RAZOR
3232
@CustomCommandResult
3333
34-
<TelerikGrid Data=@GridData
35-
EditMode="@GridEditMode.Inline"
34+
<TelerikGrid Data=@GridData
35+
EditMode="@GridEditMode.Inline"
3636
OnUpdate="@HandleUpdate"
37-
Pageable="true"
38-
PageSize="15"
37+
Pageable="true"
38+
PageSize="15"
3939
Height="500px">
4040
<GridColumns>
4141
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="Employee ID" />
@@ -58,7 +58,7 @@ To conditionally show or hide command buttons in a Grid, use the context paramet
5858
5959
@code {
6060
private List<SampleData> GridData { get; set; }
61-
private MarkupString CustomCommandResult;
61+
private MarkupString CustomCommandResult { get; set; }
6262
6363
public class SampleData
6464
{
@@ -104,15 +104,12 @@ To conditionally show or hide command buttons in a Grid, use the context paramet
104104
{
105105
if (_data.Count < 1)
106106
{
107-
for (int i = 1; i < 50; i++)
107+
_data = Enumerable.Range(1, 50).Select(i => new SampleData
108108
{
109-
_data.Add(new SampleData()
110-
{
111-
ID = i,
112-
Name = "name " + i,
113-
HireDate = DateTime.Now.AddDays(-i)
114-
});
115-
}
109+
ID = i,
110+
Name = $"name {i}",
111+
HireDate = DateTime.Now.AddDays(-i)
112+
}).ToList();
116113
}
117114
118115
return await Task.FromResult(_data);
@@ -133,6 +130,108 @@ To conditionally show or hide command buttons in a Grid, use the context paramet
133130
### Note
134131
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.
135132

133+
````RAZOR
134+
<style>
135+
.hide-command-button {
136+
display: none;
137+
}
138+
</style>
139+
140+
@CustomCommandResult
141+
142+
<TelerikGrid Data=@GridData
143+
EditMode="@GridEditMode.Inline"
144+
OnUpdate="@HandleUpdate"
145+
Pageable="true"
146+
PageSize="15"
147+
Height="500px">
148+
<GridColumns>
149+
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="Employee ID" />
150+
<GridColumn Field=@nameof(SampleData.Name) Title="Employee Name" />
151+
<GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date" />
152+
<GridCommandColumn Context="dataItem">
153+
@{
154+
var item = (SampleData)dataItem;
155+
}
156+
<GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">Edit</GridCommandButton>
157+
<GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true" OnClick="@HandleCustomSave">Save</GridCommandButton>
158+
<GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton>
159+
<GridCommandButton Command="MyOwnCommand" Class="@(item.ID % 2 != 0 ? "hide-command-button" : string.Empty)" Icon="@SvgIcon.InfoCircle" ShowInEdit="false" OnClick="@HandleCustomButtonClick">My Command</GridCommandButton>
160+
</GridCommandColumn>
161+
</GridColumns>
162+
</TelerikGrid>
163+
164+
@code {
165+
private List<SampleData> GridData { get; set; }
166+
private MarkupString CustomCommandResult { get; set; }
167+
168+
public class SampleData
169+
{
170+
public int ID { get; set; }
171+
public string Name { get; set; }
172+
public DateTime HireDate { get; set; }
173+
}
174+
175+
private async Task HandleCustomSave(GridCommandEventArgs args)
176+
{
177+
SampleData theUpdatedItem = args.Item as SampleData;
178+
}
179+
180+
private async Task HandleCustomButtonClick(GridCommandEventArgs args)
181+
{
182+
CustomCommandResult = new MarkupString(CustomCommandResult + string.Format("<br />Custom command triggered for item {0}", (args.Item as SampleData).ID));
183+
}
184+
185+
private async Task HandleUpdate(GridCommandEventArgs args)
186+
{
187+
SampleData theUpdatedItem = args.Item as SampleData;
188+
189+
await MyService.Update(theUpdatedItem);
190+
191+
await GetGridData();
192+
}
193+
194+
private async Task GetGridData()
195+
{
196+
GridData = await MyService.Read();
197+
}
198+
199+
protected override async Task OnInitializedAsync()
200+
{
201+
await GetGridData();
202+
}
203+
204+
public static class MyService
205+
{
206+
private static List<SampleData> _data { get; set; } = new List<SampleData>();
207+
208+
public static async Task<List<SampleData>> Read()
209+
{
210+
if (_data.Count < 1)
211+
{
212+
_data = Enumerable.Range(1, 50).Select(i => new SampleData
213+
{
214+
ID = i,
215+
Name = $"name {i}",
216+
HireDate = DateTime.Now.AddDays(-i)
217+
}).ToList();
218+
}
219+
220+
return await Task.FromResult(_data);
221+
}
222+
223+
public static async Task Update(SampleData itemToUpdate)
224+
{
225+
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
226+
if (index != -1)
227+
{
228+
_data[index] = itemToUpdate;
229+
}
230+
}
231+
}
232+
}
233+
````
234+
136235
## See Also
137236
* [Grid Overview](slug://grid-overview)
138237
* [Grid Command Column](slug://components/grid/columns/command)

0 commit comments

Comments
 (0)