Skip to content

Commit 7086f89

Browse files
docs(grid): group templates examples
1 parent ce49f09 commit 7086f89

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed
45.7 KB
Loading
52.6 KB
Loading

components/grid/templates.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,149 @@ Bound columns render the name of the field or their `Title` in their header. Thr
309309
310310
## Column Group Footer
311311

312-
When the grid is grouped, the columns can display a footer with information about the column data and some custom text/logic. The template is strongly typed and exposes the available aggregates values. For more information and examples, see the [Aggregates]({%slug grid-aggregates%}) article.
312+
When the grid is grouped, the columns can display a footer with information about the column data [aggregates]({%slug grid-aggregates%}) and some custom text/logic. The template is strongly typed and exposes the available aggregates values.
313+
314+
>caption Sample Column Group Footer Template
315+
316+
````CSHTML
317+
@* Group by the Team column to see the results and aggregate data in the footer *@
318+
319+
<TelerikGrid Data=@GridData Groupable="true" Pageable="true" Height="650px">
320+
<GridAggregates>
321+
<GridAggregate Field=@nameof(Employee.Team) Aggregate="@GridAggregateType.Count" />
322+
<GridAggregate Field=@nameof(Employee.Salary) Aggregate="@GridAggregateType.Max" />
323+
<GridAggregate Field=@nameof(Employee.Salary) Aggregate="@GridAggregateType.Sum" />
324+
</GridAggregates>
325+
<GridColumns>
326+
<GridColumn Field=@nameof(Employee.Name) Groupable="false" />
327+
<GridColumn Field=@nameof(Employee.Team) Title="Team">
328+
<GroupFooterTemplate>
329+
Team Members: <strong>@context.Count</strong>
330+
</GroupFooterTemplate>
331+
</GridColumn>
332+
<GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false">
333+
<GroupFooterTemplate>
334+
@* you can use a group footer for non-groupable columns as well *@
335+
Total montly salary: @context.Sum
336+
<br />
337+
<span style="color: red;">Top paid employee: @context.Max</span>
338+
</GroupFooterTemplate>
339+
</GridColumn>
340+
<GridColumn Field=@nameof(Employee.ActiveProjects) Title="Active Projects">
341+
</GridColumn>
342+
</GridColumns>
343+
</TelerikGrid>
344+
345+
@code {
346+
public List<Employee> GridData { get; set; }
347+
348+
protected override void OnInitialized()
349+
{
350+
GridData = new List<Employee>();
351+
var rand = new Random();
352+
for (int i = 0; i < 15; i++)
353+
{
354+
Random rnd = new Random();
355+
GridData.Add(new Employee()
356+
{
357+
EmployeeId = i,
358+
Name = "Employee " + i.ToString(),
359+
Team = "Team " + i % 3,
360+
Salary = rnd.Next(1000, 5000),
361+
ActiveProjects = i % 4 == 0 ? 2 : 5
362+
});
363+
}
364+
}
365+
366+
public class Employee
367+
{
368+
public int EmployeeId { get; set; }
369+
public string Name { get; set; }
370+
public string Team { get; set; }
371+
public decimal Salary { get; set; }
372+
public int ActiveProjects { get; set; }
373+
}
374+
}
375+
````
376+
377+
>caption The result from the code snippet above after grouping by the `Team` column
378+
379+
![](images/column-group-footer-template.png)
313380

314381
## Group Header
315382

316383
When the grid is grouped, the top row above the group provides information about the current group value by default. You can use this template to add custom content there in addition to the current value. For more information and examples, see the [Aggregates]({%slug grid-aggregates%}) article.
317384

385+
>caption Sample Group Header Template
386+
387+
````CSHTML
388+
@* Group by the Team and Active Projects fields to see the results *@
389+
390+
<TelerikGrid Data=@GridData Groupable="true" Pageable="true" Height="650px">
391+
<GridAggregates>
392+
<GridAggregate Field=@nameof(Employee.Team) Aggregate="@GridAggregateType.Count" />
393+
</GridAggregates>
394+
<GridColumns>
395+
<GridColumn Field=@nameof(Employee.Name) Groupable="false" />
396+
<GridColumn Field=@nameof(Employee.Team) Title="Team">
397+
<GroupHeaderTemplate>
398+
@context.Value @* the default text you would get without the template *@
399+
&nbsp;<span>Team size: @context.Count</span>
400+
</GroupHeaderTemplate>
401+
</GridColumn>
402+
<GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false" />
403+
<GridColumn Field=@nameof(Employee.ActiveProjects) Title="Active Projects">
404+
<GroupHeaderTemplate>
405+
@{
406+
<span>Currently active projects: @context.Value &nbsp;</span>
407+
408+
//sample of conditional logic in the group header
409+
if ( (int)context.Value > 3) // in a real case, ensure type safety and add defensive checks
410+
{
411+
<strong style="color: red;">These people work on too many projects</strong>
412+
}
413+
}
414+
</GroupHeaderTemplate>
415+
</GridColumn>
416+
</GridColumns>
417+
</TelerikGrid>
418+
419+
@code {
420+
public List<Employee> GridData { get; set; }
421+
422+
protected override void OnInitialized()
423+
{
424+
GridData = new List<Employee>();
425+
var rand = new Random();
426+
for (int i = 0; i < 15; i++)
427+
{
428+
Random rnd = new Random();
429+
GridData.Add(new Employee()
430+
{
431+
EmployeeId = i,
432+
Name = "Employee " + i.ToString(),
433+
Team = "Team " + i % 3,
434+
Salary = rnd.Next(1000, 5000),
435+
ActiveProjects = i % 4 == 0 ? 2 : 5
436+
});
437+
}
438+
}
439+
440+
public class Employee
441+
{
442+
public int EmployeeId { get; set; }
443+
public string Name { get; set; }
444+
public string Team { get; set; }
445+
public decimal Salary { get; set; }
446+
public int ActiveProjects { get; set; }
447+
}
448+
}
449+
````
450+
451+
>caption The result from the code snippet above after grouping by the `Team` and `Active Projects` columns
452+
453+
![](images/group-header-template.png)
454+
318455
## See Also
319456

320457
* [Live Demo: Grid Templates](https://demos.telerik.com/blazor-ui/grid/templates)

0 commit comments

Comments
 (0)