-
Notifications
You must be signed in to change notification settings - Fork 81
Added new kb article grid-show-tooltip-on-column-header #2756
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
Tsvetomir-Hr
merged 13 commits into
master
from
new-kb-grid-show-tooltip-on-column-header-b24a79505f98413da0f535dd9072a92d
Mar 5, 2025
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0281fa7
Added new kb article grid-show-tooltip-on-column-header
ebfbc4a
chore: polish kb
Tsvetomir-Hr 005f351
chore: update kb example and apply suggestions
Tsvetomir-Hr f6d8949
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr b9b992b
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr ba4f572
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 38a188d
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 7bd0566
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 6a3d413
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 49eb4a9
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 525129d
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 990340b
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 8d32a6b
Update knowledge-base/grid-show-tooltip-on-column-header.md
Tsvetomir-Hr 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
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,130 @@ | ||
| --- | ||
| title: Adding Tooltips to Grid Column Headers with Ellipsis in Blazor | ||
| description: Learn how to display tooltips on Blazor Grid column headers when the text is truncated due to column resizing. | ||
| type: how-to | ||
| page_title: How to Show Tooltips on Truncated Grid Column Headers in Blazor | ||
| slug: grid-kb-show-tooltip-on-column-header | ||
| tags: grid, blazor, tooltip, column header, template, ellipsis | ||
| res_type: kb | ||
| ticketid: 1677858 | ||
| --- | ||
|
|
||
| ## Description | ||
|
|
||
| When the column headers in a [Grid for Blazor](slug:components/grid/) are too long for the column width, they may be truncated with an ellipsis. In such cases, adding tooltips to the headers can help display the full text. | ||
|
|
||
| This knowledge-base article also answers the following questions: | ||
|
|
||
| - How to add Tooltip to Grid column header in Blazor? | ||
| - How to show Tooltip only for truncated column headers in a Blazor Grid? | ||
| - How to customize Grid column headers in Blazor? | ||
|
|
||
| ## Solution | ||
Tsvetomir-Hr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| To display Tooltip for Grid column headers that are truncated, follow the steps below: | ||
|
|
||
| 1. Use the [Column Header Template](slug:components/grid/templates/column-header#column-header-template) to customize the header content. Wrap the header content in a `<span>` HTML element. | ||
| 2. Monitor the column width changes by utilizing the [Grid State](slug:components/grid/state) and its [ColumnState](slug:components/grid/state#information-in-the-grid-state) property. | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 3. Use [TelerikTooltip](slug:components/tooltip/overview) component to display tooltip for each column header. | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 4. Apply a custom CSS class to the column header content when the width of the column is insufficient to display its full content. | ||
|
|
||
| >caption Show TelerikTooltip on the Grid column header | ||
|
|
||
| ````RAZOR | ||
| <strong>Reduce the width of some columns to observe the expected result</strong> | ||
| <br/> | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <TelerikGrid Data="@MyData" Resizable="true" | ||
| OnStateChanged="@((GridStateEventArgs<SampleData> args) => HandleColumnWidthChange(args))" | ||
| @ref="GridRef" | ||
| Reorderable="true" Width="510px"> | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <GridColumns> | ||
| <GridColumn Field="@(nameof(SampleData.Id))" Width="130px"> | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <HeaderTemplate> | ||
| <span title="Unique Identifier" | ||
| class="@(ShowTooltip.GetValueOrDefault(nameof(SampleData.Id), false) ? "employee-header" : "")"> | ||
| Unique Identifier | ||
| </span> | ||
| </HeaderTemplate> | ||
| </GridColumn> | ||
|
|
||
| <GridColumn Field="@(nameof(SampleData.Name))" Width="165px"> | ||
| <HeaderTemplate> | ||
| <span title="Long Employee Name" | ||
| class="@(ShowTooltip.GetValueOrDefault(nameof(SampleData.Name), false) ? "employee-header" : "")"> | ||
| Long Employee Name | ||
| </span> | ||
| </HeaderTemplate> | ||
| </GridColumn> | ||
|
|
||
| <GridColumn Field="@(nameof(SampleData.Team))" Width="210px"> | ||
| <HeaderTemplate> | ||
| <span title="Extremely Long Team Name" | ||
| class="@(ShowTooltip.GetValueOrDefault(nameof(SampleData.Team), false) ? "employee-header" : "")"> | ||
| Extremely Long Team Name | ||
| </span> | ||
| </HeaderTemplate> | ||
| </GridColumn> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| <TelerikTooltip TargetSelector=".employee-header" Class="my-callout"> | ||
| </TelerikTooltip> | ||
|
|
||
| <style> | ||
| .my-callout .k-callout { | ||
| left: 30px !important; | ||
| } | ||
| </style> | ||
Tsvetomir-Hr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @code { | ||
| private TelerikGrid<SampleData> GridRef { get; set; } = null!; | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private IEnumerable<SampleData> MyData = Enumerable.Range(1, 10).Select(x => new SampleData | ||
| { | ||
| Id = x, | ||
| Name = "name " + x, | ||
| Team = "team" + x | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| // Define minimum width requirements for tooltips | ||
| private static readonly Dictionary<string, double> MinColumnWidths = new() | ||
| { | ||
| { nameof(SampleData.Id), 125 }, | ||
| { nameof(SampleData.Name), 160 }, | ||
| { nameof(SampleData.Team), 205 } | ||
| }; | ||
|
|
||
| // Store whether a tooltip should be shown for each column | ||
| private Dictionary<string, bool> ShowTooltip = new(); | ||
|
|
||
| private async Task HandleColumnWidthChange(GridStateEventArgs<SampleData> args) | ||
| { | ||
| foreach (var column in args.GridState.ColumnStates) | ||
| { | ||
| string columnField = column.Field; | ||
| if (MinColumnWidths.TryGetValue(columnField, out double minWidth)) | ||
| { | ||
| double currentWidth = double.Parse(column.Width.Replace("px", "")); | ||
| ShowTooltip[columnField] = currentWidth < minWidth; | ||
| } | ||
| } | ||
|
|
||
| await GridRef.SetStateAsync(args.GridState); | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public class SampleData | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| public string Team { get; set; } = string.Empty; | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| > The additional CSS is used to adjust the position of the tooltip callout. Modify this CSS based on your application's specific layout and design requirements. | ||
|
|
||
| ## See Also | ||
Tsvetomir-Hr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [Grid Column Header Template Documentation](slug:components/grid/templates/column-header#column-header-template) | ||
| - [Telerik Tooltip Overview](slug:components/tooltip/overview) | ||
| - [Tooltip Template Feature](slug:components/tooltip/template) | ||
Tsvetomir-Hr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [Hide the Tooltip Callout or Change Its Position](slug:tooltip-callout-position) | ||
| - [Show Tooltip in Grid](slug:tooltip-in-grid) | ||
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.