简体   繁体   English

Angular材料设计动态表

[英]Angular material design dynamic table

I am trying create in Angular 9 dynamic table.我正在尝试在 Angular 9 动态表中创建。

I have html:我有 html:

<div *ngFor="let column of columns">
  <ng-container matColumnDef="{{column.columnSearchName}}">
    <mat-header-cell *matHeaderCellDef>
      <span mat-sort-header> <div [innerHTML]="column.columnName"></div></span>
      <span><input matInput class="filter-input" placeholder=""/></span>
    </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{insertValueInTable(element, column)}} </mat-cell>
  </ng-container>
</div>

<mat-header-row *matHeaderRowDef="(displayedColumns$)"></mat-header-row>
<mat-row *matRowDef="let row; columns: (displayedColumns$);"></mat-row>

and ts:和 ts:

import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { GardenListService } from '../service/garden-list.service';
import { GardenDataSource } from '../dataSource/GardenDataSource';
import { MatSort } from '@angular/material/sort';
import { Column } from './class/column';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit, AfterViewInit {

  data = new GardenDataSource(this.gardenService);
  displayedColumns$: string[] = [];

  @ViewChild(MatSort) sort: MatSort;

  columns: Column[] = [];

  constructor(private gardenService: GardenListService) {
  }

  ngOnInit(): void {
    this.columns = [
      {
        columnName: 'Číslo',
        columnSearchName: 'number',
      }, {
        columnName: 'Příjmení',
        columnSearchName: 'person.lastName',
      }, {
        columnName: 'Jméno',
        columnSearchName: 'person.firstName',
      }, {
        columnName: 'Město',
        columnSearchName: 'person.address.city',
      }, {
        columnName: 'Ulice',
        columnSearchName: 'person.address.street',
      }, {
        columnName: 'Číslo popisné',
        columnSearchName: 'person.address.streetNumber',
      }, {
        columnName: 'Číslo parcely',
        columnSearchName: 'plotNumber',
      }, {
        columnName: 'Plocha m&#178;',
        columnSearchName: 'area',
      }, {
        columnName: 'Nájemné',
        columnSearchName: 'rent',
        currencyType: true,
      }
    ];

    this.columns.forEach(col => this.displayedColumns$.push(col && col.columnSearchName));

  }

  ngAfterViewInit(): void {

    this.sort.sortChange.subscribe(() => {
      this.gardenService.getAll(this.sort.active, this.sort.direction).subscribe((response) => {
        this.data = response;
      });
    });
  }

  insertValueInTable(element: any, column: Column): string {
      return element[column.columnSearchName];
  }
}

Data I get from backend.我从后端获得的数据。 Data are load and when I want show it in table I see filled only a few columns.数据已加载,当我想在表格中显示它时,我看到只填充了几列。

Columns which contains columnSearchName with dot are empty.包含带有点的columnSearchName的列是空的。 Columns withou dot are ok.没有点的列是可以的。

When I create in html file for every matColumnDef with inserted columnSearchName with dot all was ok.当我在 html 文件中为每个 matColumnDef 创建带有点的 columnSearchName 时,一切正常。 But dynamicky it not working.但是动态它不起作用。

How I can show data which have in columnSearchName dot.如何显示columnSearchName点中的数据。

Thank you谢谢

You can also try creating dynamic table this way, it works fine in your case for the value "person.firstName" or any value with dot.您也可以尝试以这种方式创建动态表,在您的情况下,它适用于值“person.firstName”或任何带点的值。

<table mat-table [dataSource]="dataSource" multiTemplateDataRows>
   <ng-container *ngFor="let column of columns" [matColumnDef]="column.header">
       <th mat-header-cell *matHeaderCellDef>{{ column.header }}
       </th>

       <td mat-cell *matCellDef="let element">{{ element[column.value] }}
       </td>
   <ng-container>
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

In the above code dataSource is my data array in table and columns is header values of my table在上面的代码中,dataSource 是我在表中的数据数组,列是我的表的 header 值

I have to split words by dot and reduce.我必须按点拆分单词并减少。 Solutions on my problem is https://stackoverflow.com/a/49295994/3468921我的问题的解决方案是https://stackoverflow.com/a/49295994/3468921

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

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