Angular SelectBox - Add Tooltips to Items
The useItemTextAsTitle property adds a title attribute to each item element with the item's display text. The browser displays this text as a native tooltip when a user hovers over the item.
- <dx-select-box
- [useItemTextAsTitle]="true"
- [items]="items"
- displayExpr="name"
- valueExpr="id">
- </dx-select-box>
- import { Component } from "@angular/core";
- import { DxSelectBoxModule } from "devextreme-angular";
- @Component({
- selector: "app-root",
- templateUrl: "./app.component.html",
- standalone: true,
- imports: [DxSelectBoxModule]
- })
- export class AppComponent {
- items = [
- { id: 1, name: "Item 1" },
- { id: 2, name: "Item 2" }
- ];
- }
If you use a custom itemTemplate, add a title attribute to the root element of the template to display the item text as a native browser tooltip:
- <dx-select-box
- [items]="items"
- displayExpr="name"
- valueExpr="id"
- itemTemplate="itemTemplate">
- <div *dxTemplate="let item of 'itemTemplate'" [title]="item.name">
- <b>{{ item.name }}</b><br/>
- <small>{{ item.email }}</small>
- </div>
- </dx-select-box>
- import { Component } from "@angular/core";
- import { DxSelectBoxModule } from "devextreme-angular";
- @Component({
- selector: "app-root",
- templateUrl: "./app.component.html",
- standalone: true,
- imports: [DxSelectBoxModule]
- })
- export class AppComponent {
- items = [
- { id: 1, name: "John Doe", email: "john@example.com" },
- { id: 2, name: "Jane Smith", email: "jane@example.com" }
- ];
- }
To display a styled Tooltip with custom content, initialize the Tooltip component inside itemTemplate:
- <dx-select-box
- [items]="items"
- displayExpr="name"
- valueExpr="id"
- itemTemplate="itemTemplate">
- <div *dxTemplate="let item of 'itemTemplate'">
- <span [id]="'item_' + item.id">{{ item.name }}</span>
- <dx-tooltip
- [target]="'#item_' + item.id"
- showEvent="mouseenter"
- hideEvent="mouseleave">
- <div *dxTemplate="let t of 'content'">{{ item.description }}</div>
- </dx-tooltip>
- </div>
- </dx-select-box>
- import { Component } from "@angular/core";
- import { DxSelectBoxModule, DxTooltipModule } from "devextreme-angular";
- @Component({
- selector: "app-root",
- templateUrl: "./app.component.html",
- standalone: true,
- imports: [DxSelectBoxModule, DxTooltipModule]
- })
- export class AppComponent {
- items = [
- { id: 1, name: "Item 1", description: "Detailed description 1" },
- { id: 2, name: "Item 2", description: "Detailed description 2" }
- ];
- }
You can replace "mouseenter" and "mouseleave" with other events such as "click", "dblclick", "focus", or "blur" to control when the Tooltip is displayed.
To display a tooltip only for truncated items, compare element.scrollWidth with element.clientWidth and set the title attribute or display the Tooltip conditionally.
To display HTML content in the tooltip, return an HTML string from contentTemplate in the Tooltip component or from itemTemplate.