选择特定行时如何将数据获取到另一个组件

发布于 2025-02-10 07:44:29 字数 895 浏览 2 评论 0原文

当我在此处使用Mat-able选择特定行时,我正在尝试获取数据,

这是我要做什么的示例,通过获得DR20220000013将传递到交付收据#

.html

<mat-table [dataSource]="DeliveryReceipt">

<ng-container matColumnDef="transactionType">
<mat-header-cell *matHeaderCellDef>Transaction #</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.transactionType}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
            <mat-row *matRowDef="let row; columns: displayedColumns;" 
(click)="selectedRow(row)"></mat-row>
          </mat-table>

.ts

selectedRow(row){
  console.log('selectedRow', row)
}

I'm trying to get the data when i'm selecting specific row using mat-table

Here the example of what i'm going to do, by getting DR20220000013 will pass to the Delivery Receipt#

HERE'S THE EXAMPLE

.HTML

<mat-table [dataSource]="DeliveryReceipt">

<ng-container matColumnDef="transactionType">
<mat-header-cell *matHeaderCellDef>Transaction #</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.transactionType}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
            <mat-row *matRowDef="let row; columns: displayedColumns;" 
(click)="selectedRow(row)"></mat-row>
          </mat-table>

.TS

selectedRow(row){
  console.log('selectedRow', row)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的她 2025-02-17 07:44:29

在组件之间共享数据的许多方法,输入绑定,服务您命名... https:// angular。 io/guide/inputs-utputs

,但是我在这里围绕着一个肢体,猜测有什么用。上覆的Mat-table选择该行似乎是对话模式。

模式可能是从父组件中开放的,很可能是交付收据#也是吗?

因此,当我们打开模式时,我们将订阅模式。当模态关闭时,我们将开始聆听模态发出的值(即当用户do selectedrow()我们关闭模态并返回值时)。然后将返回值捕获并馈送到交货收据#

父级中

  constructor(public dialog: MatDialog) {}

  deliveryReciept: any; 

  // here we open the modal, and pass in data for mat-table
  // your modal name is probably different
  openDialog(): void {
    const dialogRef = this.dialog.open(DeliveryRecieptDialog, { 
      width: '250px',
      data: {DeliveryReceipt: this.DeliveryReceipt, selectedReceipt: null},
    });

    // here we listen to what user selected
    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
      this.deliveryReciept = result.selectedReceipt;
    });
  }

在模式中的


  DeliveryReceipt: any;
  selectedReceipt: any;

  constructor(
    public dialogRef: MatDialogRef<DeliveryRecieptDialog>,
    @Inject(MAT_DIALOG_DATA) public data: DialogData,
  ) {

  this.DeliveryReceipt = data.DeliveryReceipt;
  this.selectedReceipt = data.selectedReceipt;
}

selectedRow(row){
  console.log('selectedRow', row);
  this.selectedReceipt = row
  this.dialogRef.close();
}

:以上是一个解释,如何从模态返回数据,然后使用打开的返回数据,您可能必须更改先前的代码才能在项目中实际工作。

Many ways to share data between components, input binding, services you name it... https://angular.io/guide/inputs-outputs

But I'm going out on a limb here and taking a guess what could work. The overlying mat-table where you select the row appears to be a dialog modal.

The modal is probably opened somewhere from the parent component, most likely where Delivery Receipt# is too?

So when we open the modal, we will subscribe to the modal. We will start listening to the value emitted by the modal when the modal closes (i.e when user does selectedRow() we close modal and return value). That return value is then catched and fed to Delivery Receipt#.

In parent

  constructor(public dialog: MatDialog) {}

  deliveryReciept: any; 

  // here we open the modal, and pass in data for mat-table
  // your modal name is probably different
  openDialog(): void {
    const dialogRef = this.dialog.open(DeliveryRecieptDialog, { 
      width: '250px',
      data: {DeliveryReceipt: this.DeliveryReceipt, selectedReceipt: null},
    });

    // here we listen to what user selected
    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
      this.deliveryReciept = result.selectedReceipt;
    });
  }

In the modal:


  DeliveryReceipt: any;
  selectedReceipt: any;

  constructor(
    public dialogRef: MatDialogRef<DeliveryRecieptDialog>,
    @Inject(MAT_DIALOG_DATA) public data: DialogData,
  ) {

  this.DeliveryReceipt = data.DeliveryReceipt;
  this.selectedReceipt = data.selectedReceipt;
}

selectedRow(row){
  console.log('selectedRow', row);
  this.selectedReceipt = row
  this.dialogRef.close();
}

The above is an explanation how to return data from modal and then use return data where it was opened from, you may have to change the previous code for it to actually work in your project.

且行且努力 2025-02-17 07:44:29

最简单的方法是使用共享服务

服务

@Injectable({
  providedIn: 'root'
})
export class TransactionService {
  transactionCode = '';
}

与表

constructor(private transactionService: TransactionService) {};

selectedRow(row){
  this.transactionService.transactionCode = row;
}

组件

constructor(private transactionService: TransactionService) {};

get transactionCode() {
   return this.transactionService.transactionCode;
}

someMethod() {
  console.log(this.transactionCode);
}
<p>{{ transactionCode }}</p>

Easiest way is to use a shared service

Service

@Injectable({
  providedIn: 'root'
})
export class TransactionService {
  transactionCode = '';
}

Component with table

constructor(private transactionService: TransactionService) {};

selectedRow(row){
  this.transactionService.transactionCode = row;
}

Any other component that needs the data

constructor(private transactionService: TransactionService) {};

get transactionCode() {
   return this.transactionService.transactionCode;
}

someMethod() {
  console.log(this.transactionCode);
}
<p>{{ transactionCode }}</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文