【问题标题】:I want to display list of users and show their respective data through graph我想显示用户列表并通过图表显示他们各自的数据
【发布时间】:2020-06-30 15:20:23
【问题描述】:

我正在显示一个用户列表,其中每个用户都有一个显示按钮。单击时,我想通过相应用户的图表显示数据。我编写了一个切换逻辑,效果很好,但是如果我单击第一个用户上的按钮,然后单击第二个用户,而不是显示第二个用户的数据,它会隐藏前一个图表但不显示当前行的任何数据。

切换逻辑(user.component.ts)

toggle() {
    this.visible = !this.visible;
    if (this.visible) {
      console.log('enter');
      this.show.emit(this.visible);
    } else {
      this.show.emit(null);
    }
  }

用户列表html代码

<div class="container">
  <table class="table">
    <thead class="thead-dark">
      <tr>
        <th scope="col">First</th>
        <th scope="col">city</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let user of users,index as i" >

          <td>{{ user.name }}</td>
          <td>{{ user.city }}</td>
          <td><button id= "i" class="btn btn-success" (click)= "toggle()">show</button></td>
         </tr>

    </tbody>
  </table>

</div>

图表

rating: Rating[] = [];
   @Input() flag = false;


  constructor(private dataService: DataService) { }

  ngOnInit() {
    console.log('init');
    var myChart= new Chart('myChart', {
      type: 'bar',
      data: {
          labels: ['Technical', 'Communication', 'Experience', 'Leadership'],
          datasets: [{
              label: 'skill scores',
              data: [10,15, 16, 22],
              backgroundColor: [
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)'

              ],
              borderColor: [
                  'rgba(255, 99, 132, 1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(75, 192, 192, 1)'

              ],
              borderWidth: 1
          }]
      },
      options: {
          scales: {
              yAxes: [{
                  ticks: {
                      beginAtZero: true
                  }
              }]
          }
      }
  });
  }



图表 html

<div style="height:40%;width:40%;" class="center" [hidden] ="!flag" >
  <canvas
  class="chart chart-bar"
  chart-data="dataChart" id  = "myChart"></canvas>
</div>

true 和 null 被传递给图形组件,该组件根据传递的值显示图形。在调试时我发现这是因为所有用户都有一个通用的切换功能。但想不出一种方法来分别为每个用户实现切换功能。希望我很清楚,任何建议

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您可以选择将布尔值 isVisible 更改为用户 ID: visibleUserId?: number

    您的切换功能类似于:

    toggle(id: number) {
        this.visibleUserId = id === this.visibleUserId ? null : id;
        this.show.emit(this.visibleUserId);
      }
    

    在标记中:

    <button id= "i" class="btn btn-success" (click)= "toggle(user.id)">show</button>
    

    现在,当Output() show 上发出一个 id 时,这就是您应该为其显示图表的用户的 id。如果它为空,则不应显示图表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-23
      • 2017-11-26
      • 2021-08-16
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多