gpt4 book ai didi

angular - 在 Angular 7 中,如何使用来自对象的数据填充垫表

转载 作者:行者123 更新时间:2023-12-05 09:15:19 28 4
gpt4 key购买 nike

目前我有以下代码来填充表格。

在 component.ts 中:

import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { MatTableDataSource } from "@angular/material/table";

@Component({
styleUrls: ["./styles.scss"],
templateUrl: "./template.html"
})
export class MyRouteData {
employeeInfoTable: object;

constructor(private http: HttpClient) {}

ngOnInit() {
this.http
.get("http://localhost:5000/MyRoute/GetEmployeeInfo")
.subscribe(response => {
this.employeeInfoTable = response;
});
}
}

我的 template.html 文件如下所示:

<mat-card style="height: 98%">
<div style="height: 95%; overflow: auto;">
<table class="MyNiceStyle">
<thead>
<tr>
<td>Name</td>
<td>Age</td>
<td>Date Of Birth</td>
<td>Address</td>
<td>Postcode</td>
<td>Gender</td>
<td>Salary</td>
<td>Job Title</td>
</tr>
</thead>

<tr *ngFor="let data of employeeInfoTable">
<td>{{data.Name}}</td>
<td>{{data.Age}}</td>
<td>{{data.DateOfBirth}}</td>
<td>{{data.Address}}</td>
<td>{{data.Postcode}}</td>
<td>{{data.Gender}}</td>
<td>{{data.Salary}}</td>
<td>{{data.JobTitle}}</td>
</table>
</div>

</mat-card>

虽然有效,但老实说,它看起来很糟糕。我想做的是使用一张垫子 table :https://material.angular.io/components/table/examples

我已将 template.html 更新为:

<mat-card style="height: 98%">
<table mat-table [dataSource]="employeeInfoTable" class="mat-elevation-z8">
<ng-container matColumnDef="Name">
<th mat-header-cell *matHeaderCellDef>Name </th>
<td mat-cell *matCellDef="let element"> {{element.Name}} </td>
</ng-container>
<ng-container matColumnDef="DateOfBirth">
<th mat-header-cell *matHeaderCellDef> DateOfBirth </th>
<td mat-cell *matCellDef="let element"> {{element.DateOfBirth}} </td>
</ng-container>
<ng-container matColumnDef="Address">
<th mat-header-cell *matHeaderCellDef> Address </th>
<td mat-cell *matCellDef="let element"> {{element.Address}} </td>
</ng-container>
<ng-container matColumnDef="Postcode">
<th mat-header-cell *matHeaderCellDef> Postcode </th>
<td mat-cell *matCellDef="let element"> {{element.Postcode}} </td>

<ng-container matColumnDef="Gender">
<th mat-header-cell *matHeaderCellDef> Gender </th>
<td mat-cell *matCellDef="let element"> {{element.Gender}} </td>
</ng-container>
<ng-container matColumnDef="Salary">
<th mat-header-cell *matHeaderCellDef> Salary </th>
<td mat-cell *matCellDef="let element"> {{element.Salary}} </td>
</ng-container>
<ng-container matColumnDef="JobTitle">
<th mat-header-cell *matHeaderCellDef> JobTitle </th>
<td mat-cell *matCellDef="let element"> {{element.JobTitle}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</mat-card>

当我运行它时,我只看到一个空白屏幕。没有控制台错误或类似的错误。我需要对 template.html 和/或 component.ts 进行哪些更改才能显示数据?

最佳答案

您需要进行这些更改。

在 component.ts 中:

import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { MatTableDataSource } from "@angular/material";
import { Object} from "../object.model";

@Component({
styleUrls: ["./styles.scss"],
templateUrl: "./template.html"
})
export class MyRouteData implements OnInit {
employeeInfoTable : Object[] = [];
employeeInfoTableDataSource = new MatTableDataSource(this.employeeInfoTable);
displayedColumns: string[] = [
"Name",
"DateOfBirth",
"Address",
"Postcode",
"Gender",
"Salary"
"JobTitle"
];

constructor(private http: HttpClient) {}

ngOnInit() {
this.http.get("http://localhost:5000/MyRoute/GetEmployeeInfo")
.subscribe(response => {
this.employeeInfoTable = response;
this.employeeInfoTableDataSource.data = this.employeeInfoTable;
});
}
}

在 object.model.ts 中

export interface Object{
id: number;
Name: string;
DateOfBirth: Date;
Address: string;
Postcode: string;
Gender: string;
Salary : number;
JobTitle : string;
}

在你的 .html 中

<mat-card style="height: 98%">
<table mat-table [dataSource]="employeeInfoTableDataSource" class="mat-elevation-z8">
<ng-container matColumnDef="Name">
<th mat-header-cell *matHeaderCellDef>Name </th>
<td mat-cell *matCellDef="let element"> {{element.Name}} </td>
</ng-container>
<ng-container matColumnDef="DateOfBirth">
<th mat-header-cell *matHeaderCellDef> DateOfBirth </th>
<td mat-cell *matCellDef="let element"> {{element.DateOfBirth}} </td>
</ng-container>
<ng-container matColumnDef="Address">
<th mat-header-cell *matHeaderCellDef> Address </th>
<td mat-cell *matCellDef="let element"> {{element.Address}} </td>
</ng-container>
<ng-container matColumnDef="Postcode">
<th mat-header-cell *matHeaderCellDef> Postcode </th>
<td mat-cell *matCellDef="let element"> {{element.Postcode}} </td>

<ng-container matColumnDef="Gender">
<th mat-header-cell *matHeaderCellDef> Gender </th>
<td mat-cell *matCellDef="let element"> {{element.Gender}} </td>
</ng-container>
<ng-container matColumnDef="Salary">
<th mat-header-cell *matHeaderCellDef> Salary </th>
<td mat-cell *matCellDef="let element"> {{element.Salary}} </td>
</ng-container>
<ng-container matColumnDef="JobTitle">
<th mat-header-cell *matHeaderCellDef> JobTitle </th>
<td mat-cell *matCellDef="let element"> {{element.JobTitle}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</mat-card>

关于angular - 在 Angular 7 中,如何使用来自对象的数据填充垫表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52656937/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com