在角形物料卡中显示不同的内容

时间:2020-02-21 22:27:38

标签: angular angular-material angular-material-7

我正在尝试重新创建此设计: Dashboard concept

我希望此仪表板根据用户选择的软件包进行更改(它将在单独的部署页面上,该页面共享用户创建的软件包)。我正在尝试为其编写全局变量,因此我只需要更改后端(它将从API抓取该数据),并仅更改每张卡的内容即可,具体取决于结果。

我似乎无法弄清楚这样做的方法,非常困惑。我曾尝试过在线搜索,但我什至不知道要搜索什么。这是我到目前为止的内容:

Angular TypeScript组件:

import {
  Component,
  OnInit
}

from '@angular/core';
import {
  StatsService
}

from '../stats.service';
@Component( {
  selector: 'app-insight', templateUrl: './insight.component.html', styleUrls: ['./insight.component.scss']
}

) export class InsightComponent implements OnInit {
  packageStats= {}
  ;
  constructor(private stats: StatsService) {}
  ;
  ngOnInit() {
    this.packageStats=this.getPackageStats();
  }
  getPackageStats() {
    return this.stats.getStats().packageA;
  }
}

后端TypeScript JSON文件组件:

export const insightStats = {
    packageA: {
        cardHeader: [
          {
            title:'Cost Entity Summary'
            // description: ''
          },
          {
            title: 'Cost Over Time',
            description: 'Cost for last day'
          },
          {
            title: 'Asset Controller',
            description: 'Number of Running Instances'
          },
          {
            title: 'Unused RI Detector',
            description: 'Number of unused Reserved Instances'
          },
          {
            title: 'Cost By Service',
            description: 'Last 30 days'
          },
          {
            title: 'Potential Savings',
            description: 'Instance Type Pricing Recommendations'
          },
          {
            title: 'Compute Instances - Daily Trend',
            description: 'Last 30 days'
          },
          {
            title: 'Storage by Department'
            // description: ''
          },
        ],
        cardBody: [
          {
            mainText: 10,
            subText: 54
          },
          {
            mainText: 53,
            subChart_type: 'line',
            subChart_data: [65, 59, 80, 81, 56, 55, 40],
            subChart_desc: 'Cost trend for the last 30 days'
          },
          {
            mainText: 228,
            subChart_type: 'line',
            subChart_data: [65, 82, 120, 10, 20, 910, 2],
            subChart_desc: 'Usage trend for the last 30 days'
          },
          {
            mainText: 0
          },
          {
            mainChart_type: 'pie',
            mainChart_data: [47.6, 35.8, 13, 3],
          },
          {
            mainChart_type: 'pie',
            mainChart_data: [24.5, 18.6, 13.9, 8.1, 1.3]
          },
          {
            mainChart_type: 'line',
            mainChart_data: [65, 82, 120, 10, 20, 910, 2]
          },
          {
            mainChart_type: 'pie',
            mainChart_data: [100]
          }
        ]
    }
  }

HTML组件:

<mat-card>
    <mat-card-title fxLayout="row" fxLayoutAlign="space-between center">
        <span>Management Dashboard</span>
        <div fxLayout="row">
            <button mat-flat-button>Default</button>
            <mat-icon>star</mat-icon>
        </div>
    </mat-card-title>
    <mat-card-content>
<div fxLayout="row wrap" fxLayoutAlign="space-between">
    <mat-card class="insight-cards" fxLayout="column" fxLayoutAlign="center center" fxFlex="0 0 calc(25% - 12px)" [ngStyle.gt-xs]="{'margin.px': 6 }" fxFlex.xs="0 0 100%" [ngStyle.xs]="{'margin-bottom.px': 10 }">
        <mat-card-title fxLayout="row" fxLayoutAlign="space-between center" class="insight-cards-title" *ngFor="let header of packageStats.cardHeader">
            <span>{{header.title}}</span>
            <mat-icon style="cursor: pointer;">info</mat-icon>
        </mat-card-title>
        <mat-card-content style="padding:10px" *ngFor="let body of packageStats.cardBody">

        <h2 *ngIf="body.hasOwnProperty('mainText')">{{body.mainText}}</h2>
        <h4 *ngIf="body.hasOwnProperty('subText')">{{body.subText}}</h4>
        </mat-card-content>
    </mat-card>
</div>

</mat-card-content>
</mat-card>

1 个答案:

答案 0 :(得分:1)

现在,您为卡标题和内容标签有单独的ngFor循环。相反,您应该在内部ngFor元素上放一个mat-card

如果您无法更改后端以结合每张卡的标题和正文,则可以转换组件文件中的数据。

<mat-card>
    <mat-card-title fxLayout="row" fxLayoutAlign="space-between center">
        <span>Management Dashboard</span>
        <div fxLayout="row">
            <button mat-flat-button>Default</button>
            <mat-icon>star</mat-icon>
        </div>
    </mat-card-title>
    <mat-card-content>
        <div fxLayout="row wrap" fxLayoutAlign="space-between">
            <mat-card *ngFor="let cardData of statCards"
                      class="insight-cards" fxLayout="column"
                      fxLayoutAlign="center center"
                      fxFlex="0 0 calc(25% - 12px)" [ngStyle.gt-xs]="{'margin.px': 6 }" fxFlex.xs="0 0 100%"
                      [ngStyle.xs]="{'margin-bottom.px': 10 }">
                <mat-card-title fxLayout="row" fxLayoutAlign="space-between center" class="insight-cards-title">
                    <span>{{cardData.header.title}}</span>
                    <mat-icon style="cursor: pointer;">info</mat-icon>
                </mat-card-title>
                <mat-card-content style="padding:10px">

                    <h2 *ngIf="cardData.body.hasOwnProperty('mainText')">{{cardData.body.mainText}}</h2>
                    <h4 *ngIf="cardData.body.hasOwnProperty('subText')">{{cardData.body.subText}}</h4>
                </mat-card-content>
            </mat-card>
        </div>

    </mat-card-content>
</mat-card>