0

我有一个 Angular 7 的应用程序,我从 api 中提取数据,将数据加载到表中。该表的末尾有一列带有一个按钮。单击该按钮调用一个函数,该函数将简单地将行数据记录到控制台。这是我的代码:

我的 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";
import {MatDialog, MatDialogRef} from '@angular/material';

@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"
     "AdditionalDetails"
  ];

   constructor(private http: HttpClient) {}

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

   displayAdditionalDetails(row) {
    console.log("Row sent from link", row);

  }
}

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

我的 template.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> Date Of Birth </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> Job Title </th>
        <td mat-cell *matCellDef="let element"> {{element.JobTitle}} </td>
    </ng-container>
    <ng-container matColumnDef="AdditionalDetails">
        <th mat-header-cell *matHeaderCellDef> AdditionalDetails </th>
        <td mat-cell *matCellDef="let element">  <button mat-icon-button color="warn" (click)="displayAdditionalDetails(element)">
          <mat-icon aria-label="Example icon-button with a heart icon">description</mat-icon>
        </button></td>
    </ng-container>

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

我现在要做的是在选择按钮时加载一个对话框。

我添加了一个名为 dialog-overview-example-dialog.html 的新 html 文件,其中包含以下内容:

<div mat-dialog-content>
    <p>“I find your lack of faith disturbing.” — Darth Vader</p>

</div>

我已将以下内容添加到我的 component.ts 文件的末尾,该文件是从某处的某个教程中获得的:

@Component({
  selector: "dialog-overview-example-dialog",
  templateUrl: "dialog-overview-example-dialog.html"
})
export class DialogOverviewExampleDialog {
  constructor(public dialogRef: MatDialogRef<DialogOverviewExampleDialog>) {}

  onCloseClick(): void {
    this.dialogRef.close();
  }
}
@Component({
  selector: 'dialog-overview-example',
  templateUrl: 'dialog-overview-example.html',
})

export class DialogOverviewExample {

  constructor(public dialog: MatDialog) {}

  openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '500px' 
    });
  }

}

我现在需要做的就是弄清楚如何从 displayAdditionalDetails 方法加载对话框。最终我会用对话框做更多的事情,但现在只加载这个框就足够了。

谢谢。

4

1 回答 1

1

要打开对话框:

  • 确保MatDialog注入你的班级
  • 使用注入的对话框,运行dialog.open(component, options)
    • component是您要在对话框中呈现的内容
    • options允许调整框,并将数据传递到对话框

例子:

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

  constructor(private dialog: MatDialog) {}

  displayAdditionalDetails(row) {
    this.dialog.open(DialogOverviewExampleDialog, {
      width: '500px',
      data: row,
    })
  }
}

这将为您打开对话框。注意data: row选项中的。这就是您将数据传递到对话框中的方式。

如果要在对话框中使用数据,则需要将 MAT_DIALOG_DATA 注入到对话框组件中,如下所示:

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog implements OnInit {

  constructor(
    private dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) private data: any)
  { }

  ngOnInit() {
    console.log(this.data); // this is the "row" passed in
  }

  onCloseClick(): void {
    this.dialogRef.close();
  }

}

如果您使用 AOT 编译,您还需要将任何呈现为对话框的组件添加到您的模块中entrycomponents

@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  entryComponents: [DialogOverviewExampleDialog],
  exports: [
    ...
  ],
})
export class MyModule {
}
于 2018-10-11T08:19:07.487 回答