|
| 1 | +--- |
| 2 | +title: Adding Tooltips to Grid Column Headers with Ellipsis in Blazor |
| 3 | +description: Learn how to display tooltips on Blazor Grid column headers when the text is truncated due to column resizing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Show Tooltips on Truncated Grid Column Headers in Blazor |
| 6 | +slug: grid-kb-show-tooltip-on-column-header |
| 7 | +tags: grid, blazor, tooltip, column header, template, ellipsis |
| 8 | +res_type: kb |
| 9 | +ticketid: 1677858 |
| 10 | +--- |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +This knowledge-base article also answers the following questions: |
| 17 | + |
| 18 | +- How to add Tooltip to Grid column header in Blazor? |
| 19 | +- How to show Tooltip only for truncated column headers in a Blazor Grid? |
| 20 | +- How to customize Grid column headers in Blazor? |
| 21 | + |
| 22 | +## Solution |
| 23 | +To display Tooltip for Grid column headers that are truncated, follow the steps below: |
| 24 | + |
| 25 | +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. |
| 26 | +2. Monitor the column width changes by utilizing the [Grid State](slug:components/grid/state) and the `ColumnState` object. |
| 27 | +3. Implement the [TelerikTooltip](slug:components/tooltip/overview) component and utilize its [Template](slug:components/tooltip/template) to define the content of the tooltip, which should match the full column header text. |
| 28 | +4. Apply a custom CSS class to the column header content when the width of the column is reduced enough to hide its full content. This class will be used as the target selector for the TelerikTooltip. |
| 29 | + |
| 30 | +### Example Implementation |
| 31 | + |
| 32 | +````RAZOR |
| 33 | +@using System.ComponentModel.DataAnnotations |
| 34 | +
|
| 35 | +<TelerikGrid Data="@MyData" Resizable="true" |
| 36 | + OnStateChanged="@((GridStateEventArgs<SampleData> args) => HandleColumnWidthChange(args))" |
| 37 | + @ref="GridRef" |
| 38 | + Width="300px"> |
| 39 | + <GridColumns> |
| 40 | + <GridColumn Field="@(nameof(SampleData.Name))" |
| 41 | + Width="165px"> |
| 42 | + <HeaderTemplate> |
| 43 | + <span class="@HeaderClass"> |
| 44 | + Long Employee Name |
| 45 | + </span> |
| 46 | + </HeaderTemplate> |
| 47 | + </GridColumn> |
| 48 | + <GridColumn Field="@(nameof(SampleData.Team))" /> |
| 49 | + </GridColumns> |
| 50 | +</TelerikGrid> |
| 51 | +
|
| 52 | +<TelerikTooltip TargetSelector=".employee-header" |
| 53 | + Class="my-callout"> |
| 54 | + <Template> |
| 55 | + Long Employee Name |
| 56 | + </Template> |
| 57 | +</TelerikTooltip> |
| 58 | +
|
| 59 | +<style> |
| 60 | + .my-callout .k-callout { |
| 61 | + left: 30px !important; |
| 62 | + } |
| 63 | +</style> |
| 64 | +
|
| 65 | +@code { |
| 66 | + private string HeaderClass { get; set; } = string.Empty; |
| 67 | + private string HeaderId { get; set; } = string.Empty; |
| 68 | + private TelerikGrid<SampleData> GridRef { get; set; } = null!; |
| 69 | +
|
| 70 | + private IEnumerable<SampleData> MyData = Enumerable.Range(1, 10).Select(x => new SampleData |
| 71 | + { |
| 72 | + Id = x, |
| 73 | + Name = "name " + x, |
| 74 | + Team = "team" + x |
| 75 | + }); |
| 76 | +
|
| 77 | + private async Task HandleColumnWidthChange(GridStateEventArgs<SampleData> args) |
| 78 | + { |
| 79 | + var employeeColumnWidth = args.GridState.ColumnStates.First(c => c.Index == 1).Width.Replace("px", ""); |
| 80 | + var columnWidth = double.Parse(employeeColumnWidth); |
| 81 | +
|
| 82 | + if (columnWidth < 160) |
| 83 | + { |
| 84 | + HeaderClass = "employee-header"; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + HeaderClass = string.Empty; |
| 89 | + } |
| 90 | +
|
| 91 | + await GridRef.SetStateAsync(args.GridState); |
| 92 | + } |
| 93 | +
|
| 94 | + public class SampleData |
| 95 | + { |
| 96 | + public int Id { get; set; } |
| 97 | + [Display(Name = "Employee Name")] |
| 98 | + public string Name { get; set; } = string.Empty; |
| 99 | + public string Team { get; set; } = string.Empty; |
| 100 | + } |
| 101 | +} |
| 102 | +```` |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +## See Also |
| 107 | +- [Grid Column Header Template Documentation](slug:components/grid/templates/column-header#column-header-template) |
| 108 | +- [Telerik Tooltip Overview](slug:components/tooltip/overview) |
| 109 | +- [Tooltip Template Feature](slug:components/tooltip/template) |
| 110 | +- [Hide the Tooltip Callout or Change Its Position](slug:tooltip-callout-position) |
0 commit comments