使用有角度的材料创建类似数学矩阵的动态垫表 [英] create dynamic mat-table like math matrix with angular material

查看:29
本文介绍了使用有角度的材料创建类似数学矩阵的动态垫表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建表时遇到一个问题.我的结构数据是动态的,这意味着行数和列数是可变的,如矩阵 m*n.和 struct 可在

I have one problem on create table. My struct data is dynamic,that means number of row and column is variable like matrix m*n. and struct is available on here.

我想将 json(上图) 显示到 ma​​t-table.但我无法正确地将数据分配给表,它在 table.show 上显示重复数据column0 用于 mat-table 的所有行.

I want to show json (above photo) to mat-table.but I can't correctly assign data to table and it show Duplicate data on table.show column0 for all rows of mat-table.

而表中的正确数据应该是

while Correct data in table should be

我的代码可在 stackblitz 上找到.

My code is available on stackblitz.

在这段代码中,我想用 5 col 和 6 行创建表.

in this code,i want create table with 5 col and 6 row.

我该如何解决并显示此表中的所有数据?

推荐答案

当你有一个矩阵时,你就有一个 FormArray 的 FormArrays,例如如果你有这样的数据

when you has a matrix you has a FormArray of FormArrays,e.g. If you has a data like

export const data=[['uno','one'],['dos','two'],['tres','three']]

您可以像 (**) 那样格式化 FormArray 的 formArray

You can format a formArray of FormArray like (**)

  myformArray = new FormArray(
        data.map(row=>new FormArray(
            row.map(x=>new FormControl(x))
  )))

当您使用 mat 表时,您需要一个displayedColumns",一个包含您想要显示的列的数组.如果你想要一个按钮删除",你可以使用另一个变量更多

When you use a mat table you need a 'displayedColumns', an array with the columns you want display. If you want has a button "delete", you can use another variable more

  displayedHeads: string[] = data[0].map((x,index)=>'col'+index);
  displayedColumns: string[] = this.displayedHeads.concat('delete')

好吧,快准备好了,我添加了一个新变量 more 来指示您拥有的列数 - 这允许我们添加新行 - 以及使用 viewChild 的表

Well, It's close to be ready, I added a new variable more that indicate the number of columns you has -this allow us add a new row-, and the table using viewChild

  columns: number = data[0].length;
  @ViewChild(MatTable, { static: true }) table: MatTable<any>; 

我们的表格已经准备好展示了:

Our table it's ready to show it:

<button mat-button (click)="add()">Add row</button>
<table mat-table [dataSource]="myformArray.controls" class="mat-elevation-z8">
  <!-- Name Column -->
  <ng-container *ngFor="let head of displayedHeads;let j=index" [matColumnDef]="head">
    <th mat-header-cell *matHeaderCellDef> {{head}} </th>
    <td mat-cell *matCellDef="let element">
      <input [formControl]="element.at(j)">
    </td>
  </ng-container>

  <ng-container matColumnDef="delete">
    <th mat-header-cell *matHeaderCellDef></th>
    <td mat-cell *matCellDef="let element;let i=index;">
      <button mat-button (click)="delete(i)">delete</button>
    </td>
  </ng-container>

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

看到 dataSource 是 myformArray.controls 并且在输入中是 [formControl]="element.at(j), yes "element 是内部 formArray"

See that the dataSource is myformArray.controls and in the input is [formControl]="element.at(j), yes "element is the inner formArray"

您可以在 stackblitz 中查看示例 (*)

我添加了两个函数来添加和删除行

I add two functions to add and remove row

  delete(index: number) {
    this.myformArray.removeAt(index);
    this.table.renderRows()

  }
  add() {
    const empty = [];
    for (let i = 0; i < this.columns; i++)
      empty.push(true)

    this.myformArray.push(
      new FormArray(empty.map(x => new FormControl('')))
    )
    this.table.renderRows()

  }

(*) 在 stackblitz 中,我添加了一个指令以允许使用箭头键跨单元格移动

(*) In the stackblitz I added a directive to allow move across cells using arrows keys

(**) 在您的情况下,您将表单 Array 格式化为

(**) In your case you format the form Array as

myformArray = new FormArray(
        data.map(row=>new FormArray(
            row.map(x=>new FormControl(x.c))
  )))

这篇关于使用有角度的材料创建类似数学矩阵的动态垫表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆