简体   繁体   English

如何使用角材料将Mat表中的选定行数据绑定到有角的弹出输入字段中

[英]How to bind a selected row data from the Mat-table in to popup input fields in angular using angular material

I have implemented a sample angular app using angular material components.I have taken a simple table in my main page with employee details. 我已经使用角材料组件实现了一个示例角应用,我在主页上做了一张简单的表格,其中包含员工详细信息。

I need to select employee row and based on the selected row I need to bind the data in the popup. 我需要选择员工行,并根据选定的行绑定弹出窗口中的数据。

can any body please help me how to access the selected row data in my popup .......? 任何机构都可以帮助我如何在弹出窗口中访问所选的行数据吗?

please access my sample app here 在这里访问我的示例应用

Any help would be greatly appreciated....! 任何帮助将不胜感激....!

You will need to inject the data into your dialog component using MAT_DIALOG_DATA... I can see this imported in your project so you seem to already be aware of this part, but may be a little confused on how to implement it... please see below. 您将需要使用MAT_DIALOG_DATA将数据注入到对话框组件中……我可以在您的项目中看到导入的数据,因此您似乎已经意识到了这一部分,但是可能对如何实现它有些困惑……请见下文。

You will also need to import inject 您还需要导入注入

import {MAT_DIALOG_DATA, MatDialogRef, MatDialog } from '@angular/material/dialog';
import { Inject } from '@angular/core';

Then in your OpenPopup class constructor setup inject MAT_DIALOG_DATA like below. 然后,在您的OpenPopup类构造函数设置中,注入MAT_DIALOG_DATA,如下所示。

Please note: Any data you pass into your dialog will need to be accessed using the data variable. 请注意:您传递给对话框的任何数据都需要使用data变量进行访问。

 export class OpenPopup {

  constructor(
    @Inject(MAT_DIALOG_DATA) public data: any,
    public dialogRef: MatDialogRef<OpenPopup> ) {

   }

Then in your openPopup() method in your TableComponent class pass the selected array into your dialog 然后在TableComponent类的openPopup()方法中,将所选数组传递到对话框中

let dialogRef = this.dialog.open(OpenPopup, {
      width: '600px', height: '400px',
      data: this.selection['_selected']
    });

Once inside of your dialog use it in the HTML like this. 一旦进入对话框,就可以像这样在HTML中使用它。

Please Note: Because this example is passing the entire selected array to your dialog the example accesses the first select row statically via the [0] index. 请注意:由于此示例将整个选定的数组传递给对话框,因此该示例通过[0]索引静态访问第一选择行。 If you intend to pass multiple values to the dialog you will need to revise your html in the dialog to handle that... if you intend to only pass a single row then revise the data variable in your openPopup() method and only pass one there. 如果您打算将多个值传递给对话框,则需要修改对话框中的html以处理该问题……如果您只想传递一行,则请修改openPopup()方法中的data变量,并且只传递一个那里。

<h3>Update Row Data</h3><br/>
{{data | json}}



<!-- -------- INPUT FIELDS ----------  --> 
<div>
    <mat-input-container floatPlaceholder="never">  
        <input matInput placeholder="First Name "  id="" name="" [value]="data[0].empName">
    </mat-input-container>
</div>
<br/><br/>

<div>
    <mat-input-container floatPlaceholder="never">  
        <input matInput placeholder="Last Name "  id="" name="" [value]="data[0].empName">
    </mat-input-container>
</div>
<br/><br/>

<div>
    <mat-input-container floatPlaceholder="never">  
        <input matInput placeholder="Title "  name="" [value]="data[0].title">
    </mat-input-container>
</div>
<br/><br/>

<div>
    <mat-input-container floatPlaceholder="never">  
        <input matInput placeholder="Address "  name="" [value]="data[0].address">
    </mat-input-container>
</div>
<br/><br/>

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

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