|
| 1 | +--- |
| 2 | +title: Tooltip Does Not Update Content with the View-Model fields and events |
| 3 | +description: The tooltip contents don't update and react to events and field values in the view-model. Why and how to fix |
| 4 | +type: troubleshooting |
| 5 | +page_title: Tooltip Does not Update According to ViewModel fields and events |
| 6 | +slug: tooltip-kb-no-update-from-main-model |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: 1490348 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Tooltip for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +When I have some dynamic content inside the tooltip template, the tooltip seems not to be re-rendering. E.g. when I have a tooltip with a text input and button and wish to disable the button when no text is entered, the tooltip seems to be only refreshing after closing and re-opening it. Calling `StateHasChanged` in the desired event does not help. |
| 27 | + |
| 28 | + |
| 29 | +## Steps to Reproduce |
| 30 | + |
| 31 | +A simplified example - show the tooltip, type in the textbox - the expected behavior is that the button will get enabled, but it does not |
| 32 | + |
| 33 | +````CSHTML |
| 34 | +@* Type in the textbox *@ |
| 35 | +
|
| 36 | +<TelerikButton Class="search-tooltip" Primary="true">Click for tooltip</TelerikButton> |
| 37 | +
|
| 38 | +<TelerikTooltip TargetSelector=".search-tooltip" Position="TooltipPosition.Bottom" ShowOn="TooltipShowEvent.Click"> |
| 39 | + <Template> |
| 40 | + <TelerikTextBox @bind-Value="@SearchText"></TelerikTextBox> |
| 41 | + <TelerikButton OnClick="@Search" Enabled="@(!string.IsNullOrEmpty(SearchText))">Search</TelerikButton> |
| 42 | + </Template> |
| 43 | +</TelerikTooltip> |
| 44 | +
|
| 45 | +@code{ |
| 46 | + string SearchText { get; set; } |
| 47 | + void Search() |
| 48 | + { |
| 49 | + Console.WriteLine("Search Click"); |
| 50 | + } |
| 51 | +} |
| 52 | +```` |
| 53 | + |
| 54 | +## Cause\Possible Cause(s) |
| 55 | + |
| 56 | +The Tooltip component renders at the root of the app to ensure its correct position - it is a direct child of the `<TelerikRootComponent>` in the main layout. |
| 57 | + |
| 58 | +This means that its content is no longer a sibling or child of the current component whose view-model you changed, and this is why calls to `StateHasChanged` do not update it. |
| 59 | + |
| 60 | + |
| 61 | +## Solution |
| 62 | + |
| 63 | +The solution is to encapsulate the desired content in its own component so that its own logic and view-model updates itself and its own rendering, and changes from the parent component also fire up its `OnParametersSet` method so it can also re-render as needed. |
| 64 | + |
| 65 | +You can expose the necessary parameters and events from it so that there are no API changes in the view-model of the main component |
| 66 | + |
| 67 | +````MainComponent |
| 68 | +@* The API is the same, the contents are in their own component, see the adjacent tab *@ |
| 69 | +
|
| 70 | +<TelerikButton Class="search-tooltip" Primary="true">Click for tooltip</TelerikButton> |
| 71 | +
|
| 72 | +<TelerikTooltip TargetSelector=".search-tooltip" Position="TooltipPosition.Bottom" ShowOn="TooltipShowEvent.Click"> |
| 73 | + <Template> |
| 74 | + <SearchTooltipContent @bind-SearchText="@SearchText" OnSearch="@Search" /> |
| 75 | + </Template> |
| 76 | +</TelerikTooltip> |
| 77 | +
|
| 78 | +@code{ |
| 79 | + string SearchText { get; set; } |
| 80 | + void Search() |
| 81 | + { |
| 82 | + Console.WriteLine("Search Click"); |
| 83 | + } |
| 84 | +} |
| 85 | +```` |
| 86 | +````SearchTooltipContent |
| 87 | +@* Sample content for the tooltip that fires up the necessary events and can update as necessary *@ |
| 88 | +
|
| 89 | +<TelerikTextBox Value="@SearchText" ValueChanged="@( (string v) => ValueChangedHandler(v) )"></TelerikTextBox> |
| 90 | +<TelerikButton OnClick="@Search" Enabled="@(!string.IsNullOrEmpty(SearchText))">Search</TelerikButton> |
| 91 | +
|
| 92 | +@code { |
| 93 | + [Parameter] |
| 94 | + public string SearchText { get; set; } |
| 95 | +
|
| 96 | + [Parameter] |
| 97 | + public EventCallback<string> SearchTextChanged { get; set; } |
| 98 | +
|
| 99 | + [Parameter] |
| 100 | + public EventCallback OnSearch { get; set; } |
| 101 | +
|
| 102 | + void ValueChangedHandler(string v) |
| 103 | + { |
| 104 | + SearchText = v; |
| 105 | + if (SearchTextChanged.HasDelegate) |
| 106 | + { |
| 107 | + SearchTextChanged.InvokeAsync(SearchText); |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | + async Task Search() |
| 112 | + { |
| 113 | + if (OnSearch.HasDelegate) |
| 114 | + { |
| 115 | + await OnSearch.InvokeAsync(null); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +```` |
| 120 | + |
0 commit comments