From 255d6cc5312050b0694dfcbf9f9bf6b49f6e9ded Mon Sep 17 00:00:00 2001 From: georgedautov Date: Mon, 28 Jul 2025 10:12:13 +0300 Subject: [PATCH] chore(example): Add new kb article combobox-multiselect-conditional-tooltip --- ...ombobox-multiselect-conditional-tooltip.md | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 knowledge-base/combobox-multiselect-conditional-tooltip.md diff --git a/knowledge-base/combobox-multiselect-conditional-tooltip.md b/knowledge-base/combobox-multiselect-conditional-tooltip.md new file mode 100644 index 000000000..6dd50e50d --- /dev/null +++ b/knowledge-base/combobox-multiselect-conditional-tooltip.md @@ -0,0 +1,223 @@ +--- +title: Display Conditional Tooltips to ComboBox and MultiSelect for Dynamic Widths +description: Learn how to conditionally display tooltips for TelerikComboBox and TelerikMultiSelect components based on dynamic text overflow. +type: how-to +page_title: Implementing Dynamic Tooltips for ComboBox and MultiSelect in Blazor +slug: combobox-multiselect-kb-conditional-tooltips +position: +tags: blazor, combobox, multiselect, tooltip +ticketid: 1693287 +res_type: kb +--- + +## Environment + + + + + + + + +
Product + ComboBox for Blazor,
+ MultiSelect for Blazor +
+ +## Description + +I want to display tooltips for the [ComboBox](https://docs.telerik.com/blazor-ui/components/combobox/overview) and [MultiSelect](https://docs.telerik.com/blazor-ui/components/multiselect/overview) components in my Blazor application only when the text is ellipsed due to dynamic widths. If there’s enough space for the text to be fully visible, the tooltip should not appear. + +This knowledge base article also answers the following questions: +- How to show tooltips for ellipsed text in ComboBox and MultiSelect? +- How to use JavaScript to check text overflow in Blazor components? +- How to implement dynamic tooltips based on clientWidth and scrollWidth? + +## Solution + +To ensure tooltips are displayed only when text is ellipsed, use JavaScript to check whether the element's `scrollWidth` exceeds its `clientWidth`. Below are the steps to implement this functionality for both components: + +### ComboBox Implementation + +1. Add a `` wrapper around the ComboBox component and assign it an id. +2. Use JavaScript to check for text overflow and conditionally set the tooltip target. + +>caption Display a Tooltip for Overflowing ComboBox Items + +```razor +@inject IJSRuntime JS + + + +@if (SelectedHobbyId != 0) +{ + +} + +@{ + + + + +} + +@code { + public int SelectedHobbyId { get; set; } + + private bool IsCurrentOverflowing { get; set; } = false; + private string CurrentHobbyName => FindCurrentHobby()?.HobbyName; + + private async Task OnMouseEnter() { + @if (SelectedHobbyId != 0) { + IsCurrentOverflowing = await JS.InvokeAsync("isTextOverflowing"); + } + } + + public HobbiesDto? FindCurrentHobby() + { + return Hobbies.FirstOrDefault(x => x.HobbyId == SelectedHobbyId); + } + + public IEnumerable Hobbies { get; set; } = new List() + { + new HobbiesDto(1, "This is a test for a very very very very long sentance."), + new HobbiesDto(2, "This is some long test sentance."), + new HobbiesDto(3, "This is another long test sentance."), + new HobbiesDto(4, "Table Tennis"), + new HobbiesDto(5, "Volleyball"), + new HobbiesDto(6, "Football"), + }; + + public class HobbiesDto + { + public HobbiesDto(int id, string name) + { + HobbyId = id; + HobbyName = name; + } + + public int HobbyId { get; set; } + public string HobbyName { get; set; } + } +} +``` + +### MultiSelect Implementation + +1. Use the `TagTemplate` to customize the display of tags in the MultiSelect. +2. Use JavaScript to determine overflow and set the tooltip. + +>caption Display a Tooltip for Overflowing MultiSelect Items + +```razor +@inject IJSRuntime JS + + + + + + + + + + + @{ + int? itemTitle = IsItemOverflowing ? context.HobbyId : null; + } + + @context.HobbyName + + + + + +@code { + private async Task OnHoverHandler(int id) + { + IsItemOverflowing = await JS.InvokeAsync("isTextOverflowing", $"item {id}"); + } + + private bool IsItemOverflowing { get; set; } + private List SelectedHobbyIds { get; set; } + private IEnumerable Hobbies { get; set; } = new List() + { + new HobbiesDto(1, "This is a test for a very very very very long sentance."), + new HobbiesDto(2, "Some long test sentance."), + new HobbiesDto(3, "Some very very very long test sentance."), + new HobbiesDto(4, "Table Tennis"), + new HobbiesDto(5, "Volleyball"), + new HobbiesDto(6, "Football"), + }; + + public class HobbiesDto + { + public int HobbyId { get; set; } + public string HobbyName { get; set; } + + public HobbiesDto(int id, string name) + { + HobbyId = id; + HobbyName = name; + } + } +} +``` + +### Notes + +- Move custom JavaScript functions to your static assets directory in production. +- Ensure the tooltip `TargetSelector` only matches elements with overflow. + +## See Also + +- [ComboBox Overview](https://docs.telerik.com/blazor-ui/components/combobox/overview) +- [MultiSelect Overview](https://docs.telerik.com/blazor-ui/components/multiselect/overview) +- [Tooltip Configuration](https://docs.telerik.com/blazor-ui/components/tooltip/overview) +- [JavaScript scrollWidth Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth) +- [JavaScript clientWidth Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth) \ No newline at end of file