How to add ToolTip to DropDownButton items
This article explains how to add a tooltip to DropDownButton items.
This can be achieved by using the beforeItemRender event in DropDownButton with the beforeRender Tooltip event as shown in the code example below.
[HTML]
<button ejs-dropdownbutton [items]='items' content="Profile" (beforeItemRender)="itemRender($event)" (beforeClose)="OnBeforeClose($event)"></button>
[TS]
import {ItemModel, MenuEventArgs, BeforeOpenCloseMenuEventArgs} from '@syncfusion/ej2-angular-splitbuttons';
import {Tooltip, TooltipEventArgs} from '@syncfusion/ej2-popups';
export class AppComponent implements OnDestroy {
public tooltip: Tooltip;
//DropDownButton items definition
public items: ItemModel[] = [
//..
];
ngAfterViewInit() {
//Initialize Tooltip component
this.tooltip = new Tooltip({
// default content of tooltip
content: 'Loading...',
// set target element to tooltip
target: '.e-custom-tooltip',
// bind beforeRender event
beforeRender: this.onBeforeRender
});
this.tooltip.appendTo('body');
}
//beforeRender event of tooltip
onBeforeRender(args: TooltipEventArgs): void {
(this as any).content = args.target.textContent;
}
//To add tooltip for DropDownButton item
public itemRender(args: MenuEventArgs) {
args.element.classList.add("e-custom-tooltip");
}
//To close tooltip while closing DropDownButton Item
OnBeforeClose(args: BeforeOpenCloseMenuEventArgs) {
this.tooltip.close();
}
//To destroy the tooltip
ngOnDestroy() {
this.tooltip.destroy();
}
}
Refer to the working sample for additional details and implementation: https://stackblitz.com/edit/angular-mj4hvc?file=app.component.html
The following screenshot displays the Tooltip in DropDownButton items,
