Skip to content

Commit 7db5c99

Browse files
committed
chore(example): Add new kb article combobox-multiselect-conditional-tooltip
1 parent 5a4d177 commit 7db5c99

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
---
2+
title: Conditional Tooltips to ComboBox and MultiSelect for Dynamic Widths
3+
description: Learn how to conditionally display tooltips for TelerikComboBox and TelerikMultiSelect components based on dynamic text overflow.
4+
type: how-to
5+
page_title: Implementing Dynamic Tooltips for ComboBox and MultiSelect in Blazor
6+
slug: combobox-multiselect-kb-conditional-tooltips
7+
position:
8+
tags: combobox, multiselect, tooltip, dynamic-width, blazor
9+
ticketid: 1693287
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tbody>
17+
<tr>
18+
<td> Product </td>
19+
<td>
20+
ComboBox for Blazor, <br/>
21+
MultiSelect for Blazor
22+
</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
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.
30+
31+
This knowledge base article also answers the following questions:
32+
- How to show tooltips for ellipsed text in ComboBox and MultiSelect?
33+
- How to use JavaScript to check text overflow in Blazor components?
34+
- How to implement dynamic tooltips based on clientWidth and scrollWidth?
35+
36+
## Solution
37+
38+
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:
39+
40+
### ComboBox Implementation
41+
42+
1. Add a `<span>` wrapper around the ComboBox component and assign it an id.
43+
2. Use JavaScript to check for text overflow and conditionally set the tooltip target.
44+
45+
Here is an example:
46+
47+
```razor
48+
@inject IJSRuntime JS
49+
50+
<script suppress-error="BL9992">
51+
window.isTextOverflowing = () => {
52+
const el = document.querySelector('.example-cb .k-input-inner');
53+
if (!el) {
54+
return;
55+
}
56+
57+
return el.scrollWidth > el.clientWidth;
58+
};
59+
</script>
60+
61+
@if (SelectedHobbyId != 0)
62+
{
63+
<TelerikTooltip TargetSelector="#products-multiselect[title]"/>
64+
}
65+
66+
@{
67+
<span
68+
id="@(IsCurrentOverflowing ? "products-multiselect" : "products-multiselect-none")"
69+
@onmouseenter="OnMouseEnter"
70+
title="@CurrentHobbyName">
71+
<TelerikComboBox @bind-Value="@SelectedHobbyId"
72+
Data="@Hobbies"
73+
Placeholder="Select your favourite sport..."
74+
TextField="@nameof(HobbiesDto.HobbyName)"
75+
ValueField="@nameof(HobbiesDto.HobbyId)"
76+
Filterable="true"
77+
Width="20%"
78+
Class="example-cb">
79+
</TelerikComboBox>
80+
</span>
81+
}
82+
83+
@code {
84+
public int SelectedHobbyId { get; set; }
85+
86+
private bool IsCurrentOverflowing { get; set; } = false;
87+
private string CurrentHobbyName => FindCurrentHobby()?.HobbyName;
88+
89+
private async Task OnMouseEnter() {
90+
@if (SelectedHobbyId != 0) {
91+
IsCurrentOverflowing = await JS.InvokeAsync<bool>("isTextOverflowing");
92+
}
93+
}
94+
95+
public HobbiesDto? FindCurrentHobby()
96+
{
97+
return Hobbies.FirstOrDefault(x => x.HobbyId == SelectedHobbyId);
98+
}
99+
100+
public IEnumerable<HobbiesDto> Hobbies { get; set; } = new List<HobbiesDto>()
101+
{
102+
new HobbiesDto(1, "This is a test for a very very very very long sentance."),
103+
new HobbiesDto(2, "This is some long test sentance."),
104+
new HobbiesDto(3, "This is another long test sentance."),
105+
new HobbiesDto(4, "Table Tennis"),
106+
new HobbiesDto(5, "Volleyball"),
107+
new HobbiesDto(6, "Football"),
108+
};
109+
110+
public class HobbiesDto
111+
{
112+
public HobbiesDto(int id, string name)
113+
{
114+
HobbyId = id;
115+
HobbyName = name;
116+
}
117+
118+
public int HobbyId { get; set; }
119+
public string HobbyName { get; set; }
120+
}
121+
}
122+
```
123+
124+
### MultiSelect Implementation
125+
126+
1. Use the `TagTemplate` to customize the display of tags in the MultiSelect.
127+
2. Use JavaScript to determine overflow and set the tooltip.
128+
129+
Example implementation:
130+
131+
```razor
132+
@inject IJSRuntime JS
133+
134+
<script suppress-error="BL9992">
135+
window.isTextOverflowing = (id) => {
136+
const el = document.getElementById(id);
137+
if (!el) {
138+
return false;
139+
}
140+
141+
return el.scrollWidth > el.clientWidth;
142+
};
143+
</script>
144+
145+
<TelerikTooltip TargetSelector=".example-ms .k-chip-label[title]">
146+
<Template>
147+
@{
148+
var title = context.Title;
149+
var hobby = Hobbies.FirstOrDefault(x => x.HobbyId == Convert.ToInt32(title));
150+
<div>
151+
@(hobby?.HobbyName)
152+
</div>
153+
}
154+
</Template>
155+
</TelerikTooltip>
156+
157+
<TelerikMultiSelect Data="@Hobbies"
158+
Class="example-ms"
159+
@bind-Value="@SelectedHobbyIds"
160+
ValueField="@nameof(HobbiesDto.HobbyId)"
161+
TextField="@nameof(HobbiesDto.HobbyName)"
162+
Placeholder="Select your favourite sport..."
163+
Id="products-multiselect" Width="300px">
164+
<TagTemplate>
165+
<span class="k-chip-content">
166+
@{
167+
int? itemTitle = IsItemOverflowing ? context.HobbyId : null;
168+
}
169+
<span
170+
class="k-chip-label"
171+
@onmouseenter="() => OnHoverHandler(context.HobbyId)"
172+
id="item @context.HobbyId"
173+
title="@title">
174+
@context.HobbyName
175+
</span>
176+
</span>
177+
</TagTemplate>
178+
</TelerikMultiSelect>
179+
180+
@code {
181+
private async Task OnHoverHandler(int id)
182+
{
183+
IsItemOverflowing = await JS.InvokeAsync<bool>("isTextOverflowing", $"item {id}");
184+
}
185+
186+
private bool IsItemOverflowing { get; set; }
187+
private List<int> SelectedHobbyIds { get; set; }
188+
private IEnumerable<HobbiesDto> Hobbies { get; set; } = new List<HobbiesDto>()
189+
{
190+
new HobbiesDto(1, "This is a test for a very very very very long sentance."),
191+
new HobbiesDto(2, "Some long test sentance."),
192+
new HobbiesDto(3, "Some very very very long test sentance."),
193+
new HobbiesDto(4, "Table Tennis"),
194+
new HobbiesDto(5, "Volleyball"),
195+
new HobbiesDto(6, "Football"),
196+
};
197+
198+
public class HobbiesDto
199+
{
200+
public int HobbyId { get; set; }
201+
public string HobbyName { get; set; }
202+
203+
public HobbiesDto(int id, string name)
204+
{
205+
HobbyId = id;
206+
HobbyName = name;
207+
}
208+
}
209+
}
210+
```
211+
212+
### Notes
213+
214+
- Move custom JavaScript functions to your static assets directory in production.
215+
- Ensure the tooltip `TargetSelector` only matches elements with overflow.
216+
217+
## See Also
218+
219+
- [ComboBox Overview](https://docs.telerik.com/blazor-ui/components/combobox/overview)
220+
- [MultiSelect Overview](https://docs.telerik.com/blazor-ui/components/multiselect/overview)
221+
- [Tooltip Configuration](https://docs.telerik.com/blazor-ui/components/tooltip/overview)
222+
- [JavaScript scrollWidth Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth)
223+
- [JavaScript clientWidth Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth)

0 commit comments

Comments
 (0)