Home:ALL Converter>Add Tooltip When Ellipsis Directive to Angular Material Select

Add Tooltip When Ellipsis Directive to Angular Material Select

Ask Time:2020-09-28T23:33:38         Author:ineedtoknow

Json Formatter

I'm working in an Angular 9 project, and I have a directive that will add a matTooltip if the element is truncated (if the element's text is too long and it's overflow ends in ellipsis).

The directive works fine with all the elements I've been using, until I added a material select. Some options in my material select will have long names, and by default material select sets a max width size and overflows text with ellipsis if too long. However, I also want users to be able to view the full option name with a tooltip.

The issue is that the directive doesn't work when I add it to the mat-option element. A tooltip will never show even if it's being truncated. My guess if I'm adding it to the wrong element, and mat-option isn't what is being truncated. But I'm unsure which element is and how to add my directive to it.

Here's a stackblitz deminng the issue: https://stackblitz.com/edit/tooltip-if-truncated-directive-issue-with-matselect?file=src/app/app.component.html

Here's my tooltip directive:

@Directive({
  selector: "[matTooltip][appTooltipIfTruncated]"
})
export class TooltipIfTruncatedDirective implements OnInit {
  constructor(private matTooltip: MatTooltip, private elementRef: ElementRef<HTMLElement>) {}

  public ngOnInit(): void {
    // Wait for DOM update
    setTimeout(() => {
      console.log("tooltip dir", this.elementRef.nativeElement);
      const element = this.elementRef.nativeElement;
      console.log(
        "tooltip dir. scroll: ",
        element.scrollWidth,
        "\t client: ",
        element.clientWidth,
        "result: ",
        element.scrollWidth <= element.clientWidth
      );
      this.matTooltip.disabled = element.scrollWidth <= element.clientWidth;
    });
  }
}

And here's how I use it with most elements (and it works):

<p [matTooltip]="'test'" appTooltipIfTruncated>This is a test.</p>

And here's how I'm adding it to a material select's options (doesn't work):

<mat-select [formControl]="selectedCountryControl" placeholder="Country">
  <mat-option *ngFor="let opt of opts" [value]="opt" [matTooltip]="'test'" appTooltipIfTruncated>{{opt}}</mat-option>
</mat-select>

I'm also unsure if there's another way to do this with material select/option (without a directive), I'm open to any ways that will give the functionality of having a tooltip when the text is too long and is truncated to my material options.

Thanks for any help you can give.

Author:ineedtoknow,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64105251/add-tooltip-when-ellipsis-directive-to-angular-material-select
Manoj TM :

tooltip is not showing because the mat-option don't actually receive focus, but it's simulated focus using aria-activedescendant.\nthis issue was raised in github(LINK), looks like they not gonna change this behaviour.",
2020-11-23T21:15:14
Sanket wani :

I am using ngbTooltip in my case. You can refer this:\nthis is HTML Code\n<tr *ngFor="let parameter of params">\n<td placement="bottom" tooltipClass="paramTooltip" container="body" [ngbTooltip]="parameter.parameterCaption">{{parameter.parameterCaptionDisplay}}</td>\n</tr>\n\nBack end is here\nfor(const param of params){\nif ( param.parameterCaption.length > 30) {\n param.parameterCaptionDisplay = param.parameterCaption.substring(0, 27) + '...';\n }else{\n param.parameterCaptionDisplay = param.parameterCaption;\n }\n}\n\nThis is how you can customize input to your material options also.",
2020-09-30T14:13:13
yy