简体   繁体   English

如何在 angular 8 中的表格中显示特定列的鼠标 hover 的特定数据

[英]How to show specific data on mouse hover of specific column into table in angular 8

I have a simple json.我有一个简单的 json。 I need to show specific related span/vehicle_name on mouse hover of specific 'status'.我需要在特定“状态”的鼠标 hover 上显示特定的相关跨度/车辆名称。 Now when I hover on status(red or yellow) on my table column both vehicle_name is showing.Here I want to show only specific vehicle_name on mouse hover of specific status.现在,当我在表格列上的状态(红色或黄色)上显示 hover 时,两个车辆名称都显示。这里我只想在特定状态的鼠标 hover 上显示特定车辆名称。 Here is the code below Demo: https://stackblitz.com/edit/angular-d8cwtw?file=src%2Fapp%2Fapp.component.ts下面是演示代码: https://stackblitz.com/edit/angular-d8cwtw?file=src%2Fapp%2Fapp.component.ts

home.component.html home.component.html

<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 (mouseover)="show()" (mouseout)="hide()">{{x.status}}</span><span *ngIf="showit">{{x.vehicle_name}}</span></td>
        </tr>
</table>
</div>

home.component.ts home.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})

export class HomeComponent implements OnInit {
imageSource :any;
statusdata1: any;
showit:boolean = false;
constructor() {}

  ngOnInit() {
    /* First data */
    this.statusdata1 = [{"vehicle_number":1,"vehicle_name":"car","status":"yellow"},{"vehicle_number":2,"vehicle_name":"bus","status":"red"}];
    console.log(this.statusdata1);
  }
  show(){
    this.showit= true; 
  }
  hide(){
      this.showit= false; 
  }
}

Good day!再会!

  1. To be honest, I can suggest to you is just move this logic to separate dummy component, with such template:老实说,我可以建议你只是将这个逻辑移动到分离虚拟组件,使用这样的模板:
<span (mouseover)="show()" (mouseout)="hide()">{{x.status}}</span>
<span *ngIf="showIt">{{x.vehicle_name}}</span>

So, in this case you showIt boolean will be bonded to particular component and you needn't do some extra logic.因此,在这种情况下,您showIt将绑定到特定组件,您无需执行一些额外的逻辑。

  1. If you don't wont to do it, I think you should have additional variable (index of table row on which mouseover event triggers) and inner your template you should have such condition:如果你不这样做,我认为你应该有额外的变量(鼠标悬停事件触发的表行的索引)并且在你的模板内部你应该有这样的条件:
<tr *ngFor="let x of statusdata1; index as idx">
  <td style="border:1px solid"><span>{{x.vehicle_number}}</span></td>
  <td style="border:1px solid">
    <span (mouseover)="show(idx)" (mouseout)="hide(idx)">{{x.status}}</span>
    <span *ngIf="showit && idx === currentIdx">{{x.vehicle_name}}</span>
  </td>
</tr>
  1. Also I can suggest to you the clearest and performant solution from my option - use data-content attribute, check it out: https://davidwalsh.name/css-content-attr另外,我可以从我的选项中向您建议最清晰、最高效的解决方案 - 使用data-content属性,检查一下: https://davidwalsh.name/css-content-attr

.html .html

<table>
  <tr *ngFor="let x of statusdata1">
    <td>
      <span>{{ x.vehicle_number }}</span>
    </td>
    <td>
      <span class="status" [attr.data-line]="x.vehicle_name">
        {{ x.status }}
      </span>
    </td>
  </tr>
</table>

.css .css

td {
  border: 1px solid;
}

span[data-line]:hover:after { 
    content: attr(data-line);
}

Demo: https://stackblitz.com/edit/angular-qowzj3演示: https://stackblitz.com/edit/angular-qowzj3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM