How to load data and show them as tooltip on mouseover - Angular
我有类似于https://stackblitz.com/angular/bvydnkvdddm的垫子表。我想为列"#"中的每条记录添加工具提示。在
1 2 3 4 | <ng-container matColumnDef="number"> <th mat-header-cell *matHeaderCellDef>#</th> <td mat-cell matTooltip={{dataFromService}} *matCellDef="let row">{{ row.number }}</td> </ng-container> |
如果我添加
1 2 3 4 | <ng-container matColumnDef="number"> <th mat-header-cell *matHeaderCellDef>#</th> <td mat-cell onmouseover="getData(row.number)" matTooltip={{dataFromService}} *matCellDef="let row">{{ row.number }}</td> </ng-container> |
但是它也失败了...
对
您的模板应为:
1 2 3 4 5 6 | <ng-container matColumnDef="number"> <th mat-header-cell *matHeaderCellDef>#</th> <td mat-cell (mouseover)="getData(row.number)" matTooltip={{dataFromService}} *matCellDef="let row">{{ row.number }}</td> </ng-container> |
在您的
1 2 3 4 5 6 | dataFromService = ''; getData(rowNum){ // use your service to get the data using the rowNum // and assign the value you want, example 'data' to dataFromService variable this.dataFromService = data; } |
建议不要为每个
这是一种思考的方法。
使用
1 2 3 4 5 | <!-- Number Column --> <ng-container matColumnDef="number"> <th mat-header-cell *matHeaderCellDef>#</th> <td mat-cell *matCellDef="let row" [matTooltip]="getToolTipData(row.number)">{{ row.number }}</td> </ng-container> |
方括号使您可以绑定到
1 2 3 4 5 6 | getToolTipData(issueId: string): string { const issue = this.data.find(i => i.number === issueId); return `Title: ${issue.title} || State: ${issue.state} || Date: ${issue.created_at}`; } |
这将返回连接行数据的
考虑拉出想要在工具提示中包含的数据以及要拉入以填充表的数据。每次将鼠标悬停在带有工具提示的列上时,都会进行HTTP调用。
好吧,我的客户项目遇到了同样的问题。但是我也想在数据之间添加折线。这就是完成的过程。单击此处演示
在您的html文件中添加此代码
1 | <img src="assets/icons/about.png" alt="borderLineHigh" width="15px" height="15px" matTooltip="{{getMoreInformation()}}" matTooltipClass="test"> |
在ts文件
中
1 2 3 4 5 | getMoreInformation(): string { return 'Address : Home \ Tel : Number';// \ represent break line here } |
在您的style.css文件中(请记住项目style.css)
1 2 3 | .test { white-space: pre-line; } |
希望它能帮助某人。 ps保持编码