Skip to content

Commit 7312a14

Browse files
kb(grid): static group
1 parent b25b246 commit 7312a14

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
title: Static Group in Grid
3+
description: How to have a static (constant) group in the grid
4+
type: how-to
5+
page_title: Static Group in Grid
6+
slug: grid-kb-static-group
7+
position:
8+
tags:
9+
ticketid: 1471559
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Grid for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
Is it possible to forbid the user to remove the grouping configured by code?
26+
27+
How to hide the groupable column (set from code) but show its Group Header Template?
28+
29+
How to prevent the user from removing a certain group setting?
30+
31+
How to ensure a group is always set for the grid?
32+
33+
I would like to not show the buttons to remove the grouping options for this grid.
34+
35+
## Solution
36+
To ensure certain grid state (like always having the grid grouped by a certain field), you can use the [grid state]({%slug grid-state%}) - through its `OnStateChanged` event, you can know what the user did and [prevent or override that action]({%slug grid-state%}#get-and-override-user-action-that-changes-the-grid).
37+
38+
You can hide the group header or [x] buttons on group indicators with CSS rules.
39+
40+
Examples of both follow below, see the code comments for details.
41+
42+
>caption Ensure the grid is always grouped by a static field set in the code
43+
44+
````CSHTML
45+
@using Telerik.DataSource;
46+
47+
<TelerikGrid Data="@MyData" Height="400px" Pageable="true" FilterMode="@GridFilterMode.FilterMenu" Groupable="true"
48+
@ref="@GridRef"
49+
OnStateInit="@((GridStateEventArgs<SampleData> args) => OnStateInitHandler(args))"
50+
OnStateChanged="@((GridStateEventArgs<SampleData> args) => OnStateChangedHandler(args))">
51+
<GridColumns>
52+
<GridColumn Field="@(nameof(SampleData.Id))" Width="120px" />
53+
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
54+
<GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
55+
<GridColumn Field="@nameof(SampleData.IsOnLeave)" Title="On Vacation" />
56+
<GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" />
57+
</GridColumns>
58+
</TelerikGrid>
59+
60+
@code {
61+
public TelerikGrid<SampleData> GridRef { get; set; }
62+
63+
// initial state
64+
async Task OnStateInitHandler(GridStateEventArgs<SampleData> args)
65+
{
66+
GridState<SampleData> desiredState = new GridState<SampleData>()
67+
{
68+
GroupDescriptors = new List<GroupDescriptor>()
69+
{
70+
new GroupDescriptor()
71+
{
72+
Member = "Team",
73+
MemberType = typeof(string)
74+
}
75+
}
76+
};
77+
78+
args.GridState = desiredState;
79+
}
80+
81+
// ensure a certain group is always there, and always first
82+
83+
async void OnStateChangedHandler(GridStateEventArgs<SampleData> args)
84+
{
85+
if (args.PropertyName == "GroupDescriptors") // grouping changed
86+
{
87+
// ensure certain state based on some condition
88+
// in this example - ensure that the Team field is always in the grouping
89+
bool isGroupedByTeam = false;
90+
foreach (GroupDescriptor item in args.GridState.GroupDescriptors)
91+
{
92+
if (item.Member == "Team")
93+
{
94+
isGroupedByTeam = true;
95+
}
96+
}
97+
if (!isGroupedByTeam)
98+
{
99+
//prepare the desired descriptor at the desired location (in this case, first)
100+
List<GroupDescriptor> desiredGroups = new List<GroupDescriptor>
101+
{
102+
new GroupDescriptor
103+
{
104+
Member = "Team",
105+
MemberType = typeof(string),
106+
}
107+
};
108+
109+
//add the ones the user had (you may want to re-arrange their to suit your needs even if the user chose another field to be the first one
110+
desiredGroups.AddRange(args.GridState.GroupDescriptors);
111+
112+
//set the new state to the grid
113+
args.GridState.GroupDescriptors = desiredGroups;
114+
await GridRef.SetState(args.GridState);
115+
}
116+
}
117+
}
118+
119+
120+
// sample data follows
121+
122+
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
123+
{
124+
Id = x,
125+
Name = "name " + x,
126+
Team = "team " + x % 5,
127+
IsOnLeave = x % 2 == 0,
128+
HireDate = DateTime.Now.AddDays(-x).Date
129+
});
130+
131+
public class SampleData
132+
{
133+
public int Id { get; set; }
134+
public string Name { get; set; }
135+
public string Team { get; set; }
136+
public bool IsOnLeave { get; set; }
137+
public DateTime HireDate { get; set; }
138+
}
139+
}
140+
````
141+
142+
>caption Sample CSS rules to hide the group header and/or the [x] buttons on group indicators
143+
144+
````CSHTML
145+
<style>
146+
/* if you add the no-group-header Class to the grid, the following rule
147+
will hide the group header so the user cannot change grouping on their own */
148+
/* .no-group-header .k-grouping-header {
149+
display: none;
150+
}*/
151+
152+
153+
/* if you add the hide-first-x-button Class to the grid, the following rule
154+
will hide the [x] button from the first group indicator, so if you always keep it as the first one
155+
the user won't be able to remove it on their own.
156+
If you remove the :first-of-type pseudoclass, you can remove the [x] buttons from all groups
157+
*/
158+
/* .hide-first-x-button .k-grouping-header .k-indicator-container:first-of-type .k-button.k-button-icon.k-bare {
159+
display: none;
160+
}*/
161+
</style>
162+
````

0 commit comments

Comments
 (0)