【问题标题】:To customize Angular material mattooltip and format data to show in tooltip自定义 Angular 材质 mattooltip 和格式数据以显示在工具提示中
【发布时间】:2020-03-23 13:14:08
【问题描述】:

我想在工具提示中以 json 格式显示数据,如图所示。

我能够在工具提示中加载数据(但不是附件图片中的格式)。

<ng-container matColumnDef="Alert">
                <th mat-header-cell *matHeaderCellDef mat-sort-header> Alert </th>
                <td mat-cell *matCellDef="let row">
                        <span matTooltip="{{(row?.conditionals)?(row.conditionals | json):''}}">{{row.Alert}}</span> 
                </td>
 </ng-container>

Stackblitz 链接。 html 和 ts 代码存在于 stackblitz 链接中。

https://stackblitz.com/edit/angular-mat-tooltip-xngsaq?file=app%2Ftooltip-overview-example.ts

当我们将鼠标悬停在第一列时,需要在工具提示条件、偏移量、警报中显示,如附图所示。

#EDIT1 我也尝试过基本的 html 工具提示,如果有人可以建议格式化数据,这对我也有用

https://stackblitz.com/edit/angular-mat-tooltip-ik3f8e?file=app%2Ftooltip-overview-example.css

供参考的so链接之一 pretty-print JSON using JavaScript

【问题讨论】:

  • 你的意思是这样的?? stackblitz.com/edit/angular-mat-tooltip-ytcdaj
  • @ManirajMurugan 我能够以您在 stackblitz 链接中完成的格式显示数据,但希望与所附图片匹配
  • 您想在警报下显示 mMessage 字段吗?
  • @errorau 我已经更新了 stackblitz ,是的,当我们将鼠标悬停在第一列时,数据会出现在工具提示中,但没有像附加图片中那样格式化,谢谢
  • 在工具提示中显示这么大的文本不是一个好主意。而是在对话框中显示它,您可以在其中自定义您想要的方式。

标签: javascript html css angular angular-material


【解决方案1】:

我想,我修好了

::ng-deep .mat-tooltip {
 font-size: 15px;
 white-space: pre-wrap;
 color: gray !important;
 background-color: rgba(255, 255, 255, .9);
 border:1px solid gray;
 max-width: unset !important;
}

https://stackblitz.com/edit/angular-mat-tooltip-g28f5w?embed=1&file=app/tooltip-overview-example.css --更新--

添加matTooltipClass

<span [matTooltipClass]="{ 'tool-tip': true }" matTooltip="{{(row?.conditionals)?(row.conditionals | json):''}}">{{row.Alert}}</span> 

then change css file like 


.tool-tip {
     font-size: 15px;
    white-space: pre-wrap;
    color: gray !important;
    background-color: rgba(255, 255, 255, .9) !important;
    border:1px solid gray;
    max-width: unset !important;

    }

对于一个按钮

 <button mat-raised-button
    [matTooltipClass]="{ 'tool-tip': true }" matTooltip="{{(row?.conditionals)?(row.conditionals | json):''}}"
    aria-label="Button that displays a tooltip when focused or hovered over">

动作

【讨论】:

  • 谢谢,我已经使用普通的 html 工具提示更新了以下链接,stackblitz.com/edit/…,如果可能的话,你能建议修改一下吗
  • @Enthu 我更新答案,如果你打开封装线它会起作用
  • 非常感谢,我接受了我点击了赏金,但它只会在 6 小时后允许,无论如何我会这样做,如果你能帮助如何在工具提示中放置一个按钮和单击它会打开一个材质对话框
  • 很好 :) ybin.me/p/835393b933c929fd#KIwXUyx/… 这里是一个按钮使用这个工具提示
【解决方案2】:

试试white-space: pre-wrap;

::ng-deep .mat-tooltip {
   font-size: 15px;
  background: green;
  white-space: pre-wrap;
}

【讨论】:

  • 谢谢,但是我也需要格式化数据(和附图一样):),现在我已经开始使用普通的html工具提示,stackblitz.com/edit/angular-mat-tooltip-vsern6?file=app/…,如果你可以建议格式化数据
  • 你可以试试这个white-space: pre;
  • 谢谢,空白:预工作,但不是我在图片中附加的方式,:),我们可以像图片中那样格式化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-07
  • 1970-01-01
  • 2020-11-25
  • 2019-07-03
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
相关资源
最近更新 更多