This is a migrated thread and some comments may be shown as answers.

Passing a custom variable to ToolTip template

7 Answers 1159 Views
Tooltip
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 23 Jan 2019, 10:33 AM

I have seen following example https://www.telerik.com/kendo-angular-ui/components/tooltip/templates/ which shows how to use a template to render custom content for a tooltip, however it only shows very basic example of passing an "anchor" to the template, I would like to pass in a variable to the template, how do I do that?

In below example, I would like to pass the dataItem from within a ngFor to the template so that I can render custom tooltip content based on the data in dataItem,

<ng-template #approvalColTooltipContentTemplate let-dataItem>
{{dataItem | json}}
</ng-template>

 

<div *ngFor="let dataItem of dataItems">
      <div kendoTooltip filter=".gridTooltip" [ tooltipTemplate]="approvalColTooltipContentTemplate">
           <span class="gridTooltip"><span class="k-icon warningGreen"></span></span>
           <span class="gridTooltip"><span class="k-icon warningOrange"></span></span
           <span class="gridTooltip"><span class="k-icon k-i-warning"></span></span>
      </div>
</div>
 

 

Thanks

7 Answers, 1 is accepted

Sort by
0
Accepted
Dimiter Topalov
Telerik team
answered on 25 Jan 2019, 08:11 AM
Hi James,

Indeed, the Tooltip typically relies on displaying the title attribute of its anchor elements much like the regular HTML tooltip. The template allows the developer to customize the content, and also to display other attributes or the content of the anchor element. There is no concept of a "dataItem" related to the rendered markup via the *ngFor loop, but you can add custom data-attributes to the rendered DOM elements, and use this to implement some custom logic for displaying the properties of the respective data item in the Tooltip, e.g.:

https://stackblitz.com/edit/angular-zlsvkh?file=app/app.component.ts

I hope this helps.

Regards,
Dimiter Topalov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
James
Top achievements
Rank 1
answered on 25 Jan 2019, 09:16 AM
Yes this helps, Thanks
0
Serhat
Top achievements
Rank 1
answered on 30 Mar 2020, 02:16 PM

Hi,

 

I am trying something similar to James, in that I would like to pass a custom value to the Tooltip, however when applied to a treelist. The idea is that there are certain properties of the array that is displayed in the treelist, that are not presented via a seperate column, but rather only when you hover over the first column of the treelist. As a reference you can see this stackblitz: http://stackblitz.com/edit/angular-rla8zp?file=app%2Fapp.component.ts

I've tried to bind the Tooltip template via the class attribute to the first column of the treelist however the attribute I am setting is not read. The final aim in the example would be to display the properties locked and lockedUser from the treelist-data that is held in the filesystem.ts. In that case the variable test would have to be replaced by a function, which looks up the value of these properties for the current gridcell.

 

Thanks a lot for any help!

0
Dimiter Topalov
Telerik team
answered on 31 Mar 2020, 08:19 AM

Hi Serhat,

There are two viable approaches to achieve the desired functionality:

1) Handle the DOM mouseover event of the TreeList wrapper element, and perform some custom logic to conditionally display the Tooltip only if the event target is an element from the first column. For example, the developer can add a custom class to all column cells via the column class option.

Then nest a custom element within the template that will have the desired data item properties as data-attributes, and access and display those attributes within the Tooltip template. Here is a sample implementation, demonstrating this approach:

https://stackblitz.com/edit/angular-rla8zp-tvbxv2?file=app/app.component.ts

2) Customize the built-in TreeList styling so that a custom (e.g. SPAN) element that will fill all space, available from the hierarchy-related icons, will be rendered within each cell. Then add the desired information as data-attributes to this SPAN, and rely on the built-in Tooltip filter property to display the tooltip over these elements only. Again, access and display the data-attributes within the Tooltip template. Here is the updated example:

https://stackblitz.com/edit/angular-rla8zp-q2qi3y?file=app/app.component.ts

I hope this helps.

Regards,
Dimiter Topalov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Serhat
Top achievements
Rank 1
answered on 31 Mar 2020, 11:35 AM

 

Hi Dimitar, thank you for the quick response, this helps a lot!

Kind regards, Serhat

0
Samuel
Top achievements
Rank 1
Iron
answered on 04 Apr 2024, 06:56 AM

Hi

 

I'm having the same issue as James above.

In the stackblitz you provided, I'm trying to pass dataItem to the template ;

 


 <span class="gridTooltip" [attr.dataTooltip]="dataItem">
    <span class="k-icon k-i-warning"></span>
  </span>

because I want to use various properties of the dataItem object

 


  <ng-template #approvalColTooltipContentTemplate let-anchor>
      {{anchor.nativeElement.getAttribute('dataTooltip').someProperty}}
    </ng-template>
Inside the template anchor.nativeElement.getAttribute('dataTooltip') is an object but i cannot access any of its properties, they seemed undefined. How would you pass the object instead of just a single property ?
0
Zornitsa
Telerik team
answered on 05 Apr 2024, 02:57 PM

Hi Samuel,

The described issue occurs since the default behavior of Angular attribute binding is to resolve the passed expression to a string. This means that the value declared in the template would be the string - "[Object Object]", not the actual data item object, which is the reason why none of its properties can be accessed. 

To overcome this behavior, I would suggest using the json pipe to convert the dataItem objects to a JSON string format when performing the attribute binding:

<span class="gridTooltip" [attr.dataTooltip]="dataItem | json">
      <span class="k-icon k-font-icon k-i-warning"></span>
</span>

After that, the JSON string should be again parsed to an object in order to access its specific properties when declaring it in the template by using the JSON.parse() method. This can be implemented in a separate function or by configuring a custom pipe:

<ng-template #approvalColTooltipContentTemplate let-anchor>
      {{parseToJson(anchor.nativeElement.getAttribute('dataTooltip')).id}}
</ng-template>
public parseToJson(dataItem): any{
     return JSON.parse(dataItem);
}

Below I am linking two StackBlitz examples for observation of both approaches:

Lastly, since font icons are used in the example from this thread, I want to mention that as the font icons are no longer delivered with the Kendo UI themes since the v14.0.0 release of Kendo UI for Angular, they now have a different setup and should be loaded separately either by using the precompiled CSS or through a CDN link.

I hope the provided information is helpful.

Regards,
Zornitsa
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Samuel
Top achievements
Rank 1
Iron
commented on 08 Apr 2024, 07:04 PM | edited

thank you very much, that helped a lot !
Tags
Tooltip
Asked by
James
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
James
Top achievements
Rank 1
Serhat
Top achievements
Rank 1
Samuel
Top achievements
Rank 1
Iron
Zornitsa
Telerik team
Share this question
or