-
Notifications
You must be signed in to change notification settings - Fork 80
Added new kb article grid-dynamically-adjusting-column-header-styles #2523
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
NansiYancheva
merged 12 commits into
master
from
new-kb-grid-dynamically-adjusting-column-header-styles-8924cc84a52a42a78430c4022f34894b
Nov 15, 2024
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f24e8b1
Added new kb article grid-dynamically-adjusting-column-header-styles
7d55f3a
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 2cdb492
docs(Grid): Add example and update after review
NansiYancheva 5fbc99b
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva e48566f
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 9517518
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 505d72f
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 463b752
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 2e42d91
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 3d5f577
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva 8f17e6b
Update knowledge-base/grid-dynamically-adjusting-column-header-styles.md
NansiYancheva de3095b
docs(Grid): update KB example after review
NansiYancheva 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
138 changes: 138 additions & 0 deletions
138
knowledge-base/grid-dynamically-adjusting-column-header-styles.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,138 @@ | ||
| --- | ||
| title: Dynamically Adjusting Column Header Cell Style in Blazor Grid | ||
| description: Learn how to dynamically change the style of a column header cell in a Telerik Blazor Grid based on condition. | ||
| type: how-to | ||
| page_title: How to Dynamically Style Column Header Cell in a Telerik Blazor Grid | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| slug: grid-dynamically-adjusting-column-header-styles | ||
| tags: grid, blazor, header, headerclass | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| res_type: kb | ||
| ticketid: 1670074 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Grid for Blazor</td> | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| I am dynamically creating Grid columns in a loop and trying to adjust the column header cell style based on some condition and value from the [column field]({%slug components/grid/columns/bound%}#data-binding). Ideally, I want to implement logic to make this adjustment based on the value in the header cell. I have not been able to achieve this in the `HeaderTemplate` tag or in any of the cell render events handlers. In other templates, I have access to the `@context` field, which would make this easy, but that doesn't seem to work in the `HeaderTemplate`. | ||
|
|
||
NansiYancheva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Solution | ||
|
|
||
| To dynamically add style to a column's header cell when dynamically creating columns in a loop, use the [`HeaderClass` parameter]({%slug components/grid/columns/bound%}#appearance) to set a class under a condition and apply different styles depending on the class. For scenarios with numerous and more complex conditions, you can create a method to determine the appropriate class. | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Note that the [`HeaderTemplate`]({%slug grid-templates-column-header%}) does not receive a context argument because it is not related to rows and models, as outlined in the [Templates Overview]({%slug components/grid/features/templates%}) of the Telerik UI for Blazor documentation. | ||
|
|
||
| ### Example | ||
|
|
||
| ````CSHTML | ||
| <TelerikGrid Data="@GridData"> | ||
| <GridColumns> | ||
| <GridColumn Field="@nameof(Product.Name)" Title="Product Name"/> | ||
| @for (int i = 0; i <= MaxYears; i++) | ||
| { | ||
| <GridColumn Field="@("Y"+i)" Title=@((StartYear + i).ToString()) DisplayFormat="{0:N}" HeaderClass="@GetHeaderClass(StartYear + i)"/> | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| <style> | ||
NansiYancheva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .very-past-year-column { | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| background-color: yellow; | ||
| } | ||
|
|
||
| .past-year-column { | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| background-color: royalblue; | ||
| } | ||
|
|
||
| .current-year-column { | ||
| background-color: pink; | ||
| } | ||
|
|
||
| .future-year-column { | ||
| background-color: red; | ||
| } | ||
| </style> | ||
|
|
||
|
|
||
| @code { | ||
| private List<Product> GridData { get; set; } | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private int MaxYears = 10; | ||
| private int StartYear = 2020; | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private string GetHeaderClass(int year) | ||
| { | ||
| switch (year) | ||
| { | ||
| case <= 2020: | ||
| return "very-past-year-column"; | ||
| case < 2024: | ||
| return "past-year-column"; | ||
| case 2024: | ||
| return "current-year-column"; | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| default: | ||
| return "future-year-column"; | ||
| } | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| GridData = new List<Product>(); | ||
|
|
||
| Random rnd = new Random(); | ||
NansiYancheva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (int i = 1; i <= 30; i++) | ||
| { | ||
| GridData.Add(new Product | ||
| { | ||
| Id = i, | ||
| Name = "Product name " + i, | ||
| Y0 = rnd.Next(100, 9999), | ||
| Y1 = rnd.Next(100, 9999), | ||
| Y2 = rnd.Next(100, 9999), | ||
| Y3 = rnd.Next(100, 9999), | ||
| Y4 = rnd.Next(100, 9999), | ||
| Y5 = rnd.Next(100, 9999), | ||
| Y6 = rnd.Next(100, 9999), | ||
| Y7 = rnd.Next(100, 9999), | ||
| Y8 = rnd.Next(100, 9999), | ||
| Y9 = rnd.Next(100, 9999), | ||
| Y10 = rnd.Next(100, 9999) | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| base.OnInitialized(); | ||
| } | ||
|
|
||
| public class Product | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } | ||
| public double? Y0 { get; set; } | ||
| public double? Y1 { get; set; } | ||
| public double? Y2 { get; set; } | ||
| public double? Y3 { get; set; } | ||
| public double? Y4 { get; set; } | ||
| public double? Y5 { get; set; } | ||
| public double? Y6 { get; set; } | ||
| public double? Y7 { get; set; } | ||
| public double? Y8 { get; set; } | ||
| public double? Y9 { get; set; } | ||
| public double? Y10 { get; set; } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Grid HeaderClass Parameter]({%slug components/grid/columns/bound%}#appearance) | ||
| - [Grid Column Header Template]({%slug grid-templates-column-header%}) | ||
| - [Templates Overview - Telerik UI for Blazor]({%slug components/grid/features/templates%}) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.