javascript - 如何在角度 8 中悬停时显示来自 json 的表列数据
问题描述
我正在使用 json 数据填充到表中。这里我在 td 中的 div 是动态创建的。这里我只需要在每行的第二个 td 中显示“status”列的第一个值(现在显示所有值)。在第一个值悬停时,我需要在一个小范围内显示所有值。这是下面的代码。我也在这里创建了一个演示https://stackblitz.com/edit/angular-o6epjq?file=src%2Fapp%2Fapp.component.ts
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
imageSource :any;
statusdata1: any;
constructor() {}
ngOnInit() {
/* First data */
this.statusdata1 = [{"vehicle_number":1,"status":"red,green"},{"vehicle_number":2,"status":"yellow,red"}];
console.log(this.statusdata1);
}
getTreatment(data) {
let str = '<div class="demooutput">'
let arr = data.split(',');
if (arr.length > 0) {
for (let i = 0; i < arr.length; i++) {
str += '<span class="' + arr[i] + '"><img src="/app/animate/' + arr[i] + '.png"/></span>'
}
}
str += '</div>'
return str
}
}
app.component.html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div>
<table>
<tr *ngFor="let x of statusdata1;">
<td style="border:1px solid"><span>{{x.vehicle_number}}</span></td>
<td style="border:1px solid"><span [innerHtml]="getTreatment(x.status)"></span></td>
</tr>
</table>
</div>
解决方案
我认为这就是你要找的:我把风格放进app.component.html
去基本上展示了它。您可以考虑为其配备一个单独的组件。
An 也做了一点重构。
样式工具提示的参考: https ://www.w3schools.com/css/css_tooltip.asp
app.component.html:
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltip-content {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltip-content {
visibility: visible;
}
</style>
<div>
<table>
<tr *ngFor="let x of statusdata1;">
<td style="border:1px solid"><span>{{x.vehicle_number}}</span></td>
<td style="border:1px solid">
<div *ngIf="x.statusAvailable" class="tooltip">
{{x.statusUrls[0]}}
<span class="tooltip-content">
<span *ngFor="let status of x.statusUrls">
<img src="{{status}}" />
</span>
</span>
</div>
</td>
</tr>
</table>
</div>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imageSource: any;
statusdata1: any;
customData: any;
ngOnInit() {
/* First data */
const jsonData = [{ "vehicle_number": 1, "status": "red,green" },
{ "vehicle_number": 2, "status": "yellow,red" }];
this.statusdata1 = this.createCustom(jsonData);
}
createCustom(data) {
return data.map(row => {
const statusAvailable = typeof row.status === 'string';
const statusUrls = statusAvailable
? row.status.split(',').map(s => this.generateUrl(s))
: [];
return {
...row,
statusAvailable,
primaryStatusUrl: statusAvailable ? statusUrls[0] : undefined,
statusUrls
}
});
}
generateUrl(status) {
return `/app/animate/${status}.png`
}
}
推荐阅读
- node.js - 在不同操作系统上从外部驱动器运行 nodeJS 应用程序
- sql - SQL 将列转换为行
- apache - 修复,让 Let's Encrypt 与 www 一起使用
- angular - Angular Output 通过 emit 发送一个值
- python - 展开 PySpark 中的字典列表
- algorithm - 如何使用循环不变量来证明这个问题的正确性,谁能帮助我理解这一点?
- java - Java 是否具有本机 AutoResetEvent 等效项?
- javascript - 100% 渲染饼图数据集——Chart.JS
- python - 如何在不影响使用python的相应行的情况下用空白交换行中的重复项?
- python-3.x - 如何通过命令行传递 Hydra 配置